cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Why does this produce an error? File I/O
Why does this produce an error? File I/O
Oct 11, 2009 at 12:10am UTC
ToffeeC
(3)
I'm wondering why the following pattern is incorrect:
string filename = "myfile.txt";
ifstream input(filename);
But
ifstream input("myfile.txt");
or
char a[] = "myfile.txt";
ifstream input(a);
do? How can I make the first case work without creating any array or having to know the value of filename?
Oct 11, 2009 at 12:38am UTC
helios
(17574)
For some reason, the C++ committee decided that std::fstream::open() only needed to take a character array for the filename parameter. The function doesn't accept std::strings.
To make it work, pass filename.c_str().
Topic archived. No new replies allowed.