cppreference.com > C++ I/O > I/O Constructors
I/O Constructors
Syntax:
  fstream( const char *filename, openmode mode );
  ifstream( const char *filename, openmode mode );
  ofstream( const char *filename, openmode mode );

The fstream, ifstream, and ofstream objects are used to do file I/O. The optional mode defines how the file is to be opened, according to the ios stream mode flags. The optional filename specifies the file to be opened and associated with the stream.

For example, the following code reads input data and appends the result to an output file.

   ifstream fin( "/tmp/data.txt" );
   ofstream fout( "/tmp/results.txt", ios::app );
   while( fin >> temp )
     fout << temp + 2 << endl;
   fin.close();
   fout.close();
		

Input and output file streams can be used in a similar manner to C++ predefined I/O streams, cin and cout.