Visual C++ 6.0 ifstream Inanity

C++ No Comments »

If you use ifstream from Visual C++ 6.0, beware of the behavior of the constructor and open member function. Consider the following code snippet:

const char* fileName = ...;
ifstream ifs(fileName);  // equivalent to ifstream ifs; ifs.open(fileName);

If fileName didn’t previously exist, the ifstream constructor will create it as a 0-byte file! If you don’t want the file to be created, use the ios::nocreate flag:

ifstream ifs(fileName, ios::in | ios::nocreate);

Whoever originally thought this was a good idea should be slapped.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in