How to use string in C ++?

3

I'm having trouble dealing with string .

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

bool ArqExiste(string Arquivo)
{
    ifstream verificacao (Arquivo.c_str());
    if(verificacao.is_open())
    {
        verificacao.close();
        return true;
    }
    else
    {
        verificacao.close();
        return false;
    }
}

int main()
{
    string file = "C:\Temp\aipflib.log";

    printf("%b", ArqExiste(file));
}

At line ifstream verificacao (Arquivo.c_str()) , gave the error:

  

variable 'std :: ifstream verification' has initializer but incomplete type

I use Codeblocks to program.

    
asked by anonymous 09.07.2015 / 17:01

1 answer

1

The problem is that you do not include the required header in the #include <fstream> case.

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;

bool ArqExiste(string Arquivo) {
    ifstream verificacao (Arquivo.c_str());
    if(verificacao.is_open()) {
        verificacao.close();
        return true;
    } else {
        verificacao.close();
        return false;
    }
}

int main() {
    string file = "C:\Temp\aipflib.log";
    printf("%b", ArqExiste(file));
}

See running (approximately) on ideone .

I also took the conio which luckily is not being used in this code. This header should never be used in modern codes.

Also consider not using c_str() there, it is not necessary.

    
09.07.2015 / 17:20