-
ifstream error
I want the following function to open a file which the name of the file is stored in file_name. However, when I tried to compile it, I received no matching function for call to 'std::basic_ifstream...... error. When I replaced the file_name with actual name of the file like "data.txt", it compiled without error. Could someone please help me out? I need the users to be able to type in the file name. Thanks in advance.
Code:
void retrieve_file(string file_name)
{
string line;
unsigned int input_index;
string input_entry;
ifstream input_file (file_name);
if(input_file.is_open())
{
""" """ """
}
}
-
ifstream can only take a C-style string as a filename. You should use:
Code:
ifstream input_file (file_name.c_str());
-
-
Thankfully a string based constructor has been added to the C++0x draft so this won't be an issue in the future.
-
>> Thankfully a string based constructor has been added to the C++0x draft so this won't be an issue in the future.
Woo! That's something that's been bugging me for ages! that .c_str() ... where is this draft, Daved?
-
Here's the post from CornedBee where I got the link. I just downloaded it from there:
http://cboard.cprogramming.com/showp...05&postcount=5