Need help opening a file from user input.

I get an error that says, "no matching function." On the line with the file.open() function.


#include <iostream>
#include <fstream>

using namespace std;

int main(){
string name;
ifstream file;

cout << "Enter the file name: ";
getline(cin, name);
file.open(name);

return 0;
}
Last edited on
That function accepts a string parameter only from C++11 onwards. If you have an old compiler, get a new one, or use the other version of the function, that accepts a char* ( file.open(name.c_str()); ) .

http://www.cplusplus.com/reference/fstream/ifstream/open/

This is the year 2018, and many good modern compilers are free. You should get one.
Last edited on
I am using c++11 and i still get that error message for some reason. But I've tried it with .c_str() and now it works fine.

thanks for the help.
Topic archived. No new replies allowed.