I have a problem whith file o/i

I wrote program that input string to a file
and I want the user to enter the file name

and I use these way

cin>>filename;

ifstream myfile (filename);

the compile gave me this

cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'

but if i use the file name directly like this

ifstream myfile ("test.txt");

it's works

please help me !!!

filename is an std::string, but the constructor for std::ifstream only takes C strings (nul-terminated char arrays). To get the C string corresponding to an std::string, call std::string::c_str(). For your case, that would be filename.c_str().

I really can't imagine what they were thinking when they defined that constructor.
10000000 thanks to you helios
10000000 thanks


lolz
Well, that was a truly worthless post. And so is this one.
Topic archived. No new replies allowed.