I am reading the book C++ Primer and at the file input output chapter it uses:
ifstream infile(ifile.c_str());
to open a file whose name is in the string ifile
.
I tried the code and it works perfectly even without c_str()
. So what is the point of using it?
Should I use c_str()
when I am trying to open a file from a command line argument? I mean which is the correct usage:
ifstream fin( argv[1] )
or
ifstream fin( argv[1].c_str() )
The constructor for ifstream
used to only take a const char *
(which is what the c_str()
method provides.
I believe that there is a new constructor for it that takes a std::string
in the upcoming standard, (edit) see this answer.
It could also be specific to your implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With