Why don't the std::fstream classes take a std::string?
By taking a C string the C++03 std::fstream
class reduced dependency on the std::string
class. In C++11, however, the std::fstream
class does allow passing a std::string
for its constructor parameter.
Now, you may wonder why isn't there a transparent conversion from a std:string
to a C string, so a class that expects a C string could still take a std::string
just like a class that expects a std::string
can take a C string.
The reason is that this would cause a conversion cycle, which in turn may lead to problems. For example, suppose std::string
would be convertible to a C string so that you could use std::string
s with fstream
s. Suppose also that C string are convertible to std::string
s as is the state in the current standard. Now, consider the following:
void f(std::string str1, std::string str2);
void f(char* cstr1, char* cstr2);
void g()
{
char* cstr = "abc";
std::string str = "def";
f(cstr, str); // ERROR: ambiguous
}
Because you can convert either way between a std::string
and a C string the call to f()
could resolve to either of the two f()
alternatives, and is thus ambiguous. The solution is to break the conversion cycle by making one conversion direction explicit, which is what the STL chose to do with c_str()
.
why don't STL ifstream and ofstream classes take std::string as filenames?
why don’t ifstream and ofstream classes take std::string as filenames?
I've seen a few sensible arguments for that (namely that this would create a dependency of the streams on strings), but frankly I believe the actual reason is that the streams are much older than the standard library and its strings.
Are there any plans to upgrade STL?
It's called C++11 and will be the new version of the standard. I don't know whether file streams changed. You could look at the final draft and find out for yourself.
Note that STL is the name for a library of containers, algorithms, and iterators, incorporated into the standard library. Also part of the standard library are strings, streams and others.
In particular, streams are not part of the STL. They are siblings.
ifstream constructor taken a char* but not a std::string
No. The constructor from std::string
was added in C++11.
Why does (i|o)fstream take a const char* parameter for a file name?
Class std::string
implements the concept of "run-time-sized resizable string". This is when this class should be used - when you need a string whose size is only known at run-time and which is run-time resizable as well. In situations when you don't need these features using std::string
is an overkill. Apparently, the authors of the library didn't think that they needed a run-time resizable string to represent a file name, so they opted for a minimalistic solution: they used a C-string where a C-string was sufficient. This is actually a very good principle for designing library interfaces: never require something that you don't really need.
It is true that these days we often see people who encourage C++ programmers to use std::string
whenever they need a string, any string. They often claim that classic C strings should be reserved to C code. In general case this is a bogus philosophy. Gratuitous use of comparatively heavy objects like std::string
is more appropriate in languages like Java, but is normally unacceptable in C++.
Yes, it is possible to get away with using std::string
all the time in some C++ applications ("it is possible to write a Java program in C++"), but in such a generic low-level library as C++ standard library forcing the user to use std::string
without a good reason (i.e. imposing unnecessary requirements) would not look good.
Should std::fstream object be defined as class member
You can look at the class member as a state
of the object.
In your example, fileStream
does nothing between invocation of File::write()
, so it doesn't hold a state
.
If you would have multiple class methods that write (or read) to the same fileStream
, it would make sense to have it as a member variable and provide open
and close
methods separately from write
.
std::fstream file not getting created while making a custom Csv file manager class
The file opened successfully when I opened it with std::fstream::app append mode.
Related Topics
How to Make This C++ Object Non-Copyable
C++11 Equivalent to Boost Shared_Mutex
What Is Dynamic Initialization of Object in C++
Inferring the Call Signature of a Lambda or Arbitrary Callable for "Make_Function"
Preventing Compiler Optimizations While Benchmarking
Why Isn't C/C++'s "#Pragma Once" an Iso Standard
What Lasts After Using Std::Move C++11
Get Pointer to Object from Pointer to Some Member
How to Truncate a Floating Point Number After a Certain Number of Decimal Places (No Rounding)
Initializer-List-Constructing a Vector of Noncopyable (But Movable) Objects
Technically, How Do Variadic Functions Work? How Does Printf Work
Cmake: Include Library Dependencies in a Static Library
Memory Allocation Char* and Char[]
Fast Color Quantization in Opencv
What Does This Variadic Template Code Do
How to Test If Preprocessor Symbol Is #Define'D But Has No Value