ifstream::open() function using a string as the parameter
12,601
In the previous version of C++ (C++03), open()
takes only a const char *
for the first parameter, instead of std::string
. The correct way of calling it would then be:
myFile.open(filename.c_str());
In current C++ (C++11) that code is fine, though, so see if you can tell your compiler to enable support for it.
Author by
SemicolonExpected
Updated on June 04, 2022Comments
-
SemicolonExpected almost 2 years
I'm trying to make a program that asks for the file that they user would like to read from, and when I try to
myfile.open(fileName)
I get the error: "no matching function for call tostd::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'
" at that line.string filename; cout<<"Enter name of file: "; cin>>filename; ifstream myFile; myFile.open(filename); //where the error occurs. myFile.close();
-
Slashr about 11 yearsEven CodeBlocks doesn't support passing a string variable to the "open" method. Had to use the ".c_str()" for it to work.