Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does (i|o)fstream take a const char* parameter for a file name?

Tags:

c++

stl

history

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.

like image 759
Seth Carnegie Avatar asked May 12 '11 00:05

Seth Carnegie


People also ask

Does ofstream append to file?

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.

Is Fstream inherited from Iostream?

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.


1 Answers

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++.

like image 85
Lightness Races in Orbit Avatar answered Oct 05 '22 10:10

Lightness Races in Orbit