Search code examples
c++visual-c++ifstreamofstream

Are ifstream and ofstream non-standard c++ headers?


Ok, so I was coding a C++ console app involving File - IO. In c++, to apply file io, you have the import one of three headers, them being <ifstream>,<ofstream>, and <fstream>. In Visual Studio Community 2015, at first when I wrote this code:

#include <ifstream>
int main()
{
    std::ifstream inf("testingifstream.txt");
}

The first thing I noticed right away, is that the Intellisense gave me an error stating that it had an error opening ifstream, as it "was not found". After posting on a different forum, I got the solution to use the <fstream> header, since it has the classes for ifstream and ofstream, and then my code worked. But I only need the ifstream class, so I think it is unnecessary that I have to bring fstream in as well. This leads me to my question, are ifstream and ofstream non-standard headers for Windows Visual Studio, and is there any way I can include them?


Solution

  • Yes, the only standard header is <fstream>, containing both ifstreamand ofstream classes (and fstream of course).

    [17.6.1.2] The C++ standard library provides 55 C++ library headers, as shown in Table 14.

    In the table, there is no <ifstream>or <ofstream>.