Why does the constructor and open
method of the std::(i|o)fstream classes take the name of a file as a parameter in the form of a const char*
instead of an std::string
? It seems like the creators of the STL would want to use what they had written instead of using the type they wrote a class to replace.
In order for us to append to a file, we must put the ofstream() function in append mode, which we explain in the next paragraph. With the full line, ofstream writer("file1. txt", ios::app);, we now create a connection to open up the file1. txt file in order to append contents to the file.
Dealing with files is similar to dealing with standard input and standard output; classes ifstream, ofstream, and fstream are derived from classes istream, ostream, and iostream, respectively.
The string
part of the library was developed after streams, and nobody thought to make the obvious modifications.
It's merely out of political and temporal reality that they never got around to this before shipping C++98, and nobody bothered bringing it up again because you could always solve it with .c_str()
.
C++0x fixes this (see 27.9.1.6).
Welcome to C++.
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