Name of filename as var

I want to open a file. The name of the file is given by user input
1
2
3
std::string inputFileName;
std::cout << "Name of the file to be converted:\n";
getline(std::cin, inputFileName); //no std:: needed 


I want to use the string inputFileName to open the file:
1
2
std::ifstream inputFile(inputFileName);
...

But this gives compile error. By trial and error I changed the code. And by using c_str() it works allright:
1
2
3
4
std::ifstream inputFile(inputFileName.c_str());
std::stringstream buffer;
buffer << inputFile.rdbuf();
std::string bufString = buffer.str();


Why is that, more precize: why doesn't the first code work (without c_str())?
Last edited on
Depends what compiler you're using.

C++98 only accepts a const char * for the filename argument.
C++11 accepts a string argument.
http://www.cplusplus.com/reference/fstream/ifstream/ifstream/


Topic archived. No new replies allowed.