cppreference.com > C++ I/O
C++ I/O

I/O Constructors
Syntax:
  #include <fstream>
  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 io stream mode flags. The optional filename specifies the file to be opened and associated with the stream.

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


bad
Syntax:
  #include <fstream>
  bool bad();

The bad() function returns true if a fatal error with the current stream has occurred, false otherwise.


clear
Syntax:
  #include <fstream>
  void clear( iostate flags = ios::goodbit );

The function clear() does two things:

The flags argument defaults to ios::goodbit, which means that by default, all flags will be cleared and ios::goodbit will be set.


close
Syntax:
  #include <fstream>
  void close();

The close() function closes the associated file stream.


eof
Syntax:
  #include <fstream>
  bool eof();

The function eof() returns true if the end of the associated input file has been reached, false otherwise.

For example, the following code reads data from an input stream in and writes it to an output stream out, using eof() at the end to check if an error occurred:

 char buf[BUFSIZE];
 do {
   in.read( buf, BUFSIZE );
   std::streamsize n = in.gcount();
   out.write( buf, n );
 } while( in.good() );
 if( in.bad() || !in.eof() ) {
   // fatal error occurred
 }
 in.close();		
		


fail
Syntax:
  #include <fstream>
  bool fail();

The fail() function returns true if an error has occurred with the current stream, false otherwise.


fill
Syntax:
  #include <fstream>
  char fill();
  char fill( char ch );

The function fill() either returns the current fill character, or sets the current fill character to ch.

The fill character is defined as the character that is used for padding when a number is smaller than the specified width(). The default fill character is the space character.


flags
Syntax:
  #include <fstream>
  fmtflags flags();
  fmtflags flags( fmtflags f );

The flags() function either returns the io stream format flags for the current stream, or sets the flags for the current stream to be f.


flush
Syntax:
  #include <fstream>
  ostream& flush();

The flush() function causes the buffer for the current output stream to be actually written out to the attached device.

This function is useful for printing out debugging information, because sometimes programs abort before they have a chance to write their output buffers to the screen. Judicious use of flush() can ensure that all of your debugging statements actually get printed.


gcount
Syntax:
  #include <fstream>
  streamsize gcount();

The function gcount() is used with input streams, and returns the number of characters read by the last input operation.


get
Syntax:
  #include <fstream>
  int get();
  istream& get( char& ch );
  istream& get( char* buffer, streamsize num );
  istream& get( char* buffer, streamsize num, char delim );
  istream& get( streambuf& buffer );
  istream& get( streambuf& buffer, char delim );

The get() function is used with input streams, and either:

For example, the following code displays the contents of a file called temp.txt, character by character:

   char ch;
   ifstream fin( "temp.txt" );
   while( fin.get(ch) )
     cout << ch;
   fin.close();		
		


getline
Syntax:
  #include <fstream>
  istream& getline( char* buffer, streamsize num );
  istream& getline( char* buffer, streamsize num, char delim );

The getline() function is used with input streams, and reads characters into buffer until either:

Those using a Microsoft compiler may find that getline() reads an extra character, and should consult the documentation on the Microsoft getline bug.


good
Syntax:
  #include <fstream>
  bool good();

The function good() returns true if no errors have occurred with the current stream, false otherwise.


ignore
Syntax:
  #include <fstream>
  istream& ignore( streamsize num=1, int delim=EOF );

The ignore() function is used with input streams. It reads and throws away characters until num characters have been read (where num defaults to 1) or until the character delim is read (where delim defaults to EOF).


open
Syntax:
  #include <fstream>
  void open( const char *filename );
  void open( const char *filename, openmode mode = default_mode );

The function open() is used with file streams. It opens filename and associates it with the current stream. The optional io stream mode flag mode defaults to ios::in for ifstream, ios::out for ofstream, and ios::in|ios::out for fstream.

If open() fails, the resulting stream will evaluate to false when used in a Boolean expression. For example:

 ifstream inputStream("file.txt");
 if( !inputStream ) {
   cerr << "Error opening input stream" << endl;
   return;
 }		
		


peek
Syntax:
  #include <fstream>
  int peek();

The function peek() is used with input streams, and returns the next character in the stream or EOF if the end of file is read. peek() does not remove the character from the stream.


precision
Syntax:
  #include <fstream>
  streamsize precision();
  streamsize precision( streamsize p );

The precision() function either sets or returns the current number of digits that is displayed for floating-point variables.

For example, the following code sets the precision of the cout stream to 5:

   float num = 314.15926535;
   cout.precision( 5 );
   cout << num;		
		

This code displays the following output:

   314.16		
		


put
Syntax:
  #include <fstream>
  ostream& put( char ch );

The function put() is used with output streams, and writes the character ch to the stream.


putback
Syntax:
  #include <fstream>
  istream& putback( char ch );

The putback() function is used with input streams, and returns the previously-read character ch to the input stream.


rdstate
Syntax:
  #include <fstream>
  iostate rdstate();

The rdstate() function returns the io stream state flags of the current stream.


read
Syntax:
  #include <fstream>
  istream& read( char* buffer, streamsize num );

The function read() is used with input streams, and reads num bytes from the stream before placing them in buffer. If EOF is encountered, read() stops, leaving however many bytes it put into buffer as they are.

For example:

   struct {
     int height;
     int width;
   } rectangle;		

   input_file.read( (char *)(&rectangle), sizeof(rectangle) );
   if( input_file.bad() ) {
     cerr << "Error reading data" << endl;
     exit( 0 );
   }		
		


seekg
Syntax:
  #include <fstream>
  istream& seekg( off_type offset, ios::seekdir origin );
  istream& seekg( pos_type position );

The function seekg() is used with input streams, and it repositions the "get" pointer for the current stream to offset bytes away from origin, or places the "get" pointer at position.


seekp
Syntax:
  #include <fstream>
  ostream& seekp( off_type offset, ios::seekdir origin );
  ostream& seekp( pos_type position );

The seekp() function is used with output streams, but is otherwise very similar to seekg().


setf
Syntax:
  #include <fstream>
  fmtflags setf( fmtflags flags );
  fmtflags setf( fmtflags flags, fmtflags needed );

The function setf() sets the io stream format flags of the current stream to flags. The optional needed argument specifies that only the flags that are in both flags and needed should be set. The return value is the previous configuration of io stream format flags.

For example:

   int number = 0x3FF;
   cout.setf( ios::dec );
   cout << "Decimal: " << number << endl;
   cout.unsetf( ios::dec );
   cout.setf( ios::hex );
   cout << "Hexadecimal: " << number << endl;		
		

Note that the preceding code is functionally identical to:

   int number = 0x3FF;
   cout << "Decimal: " << number << endl << hex << "Hexadecimal: " << number << dec << endl;		
		

thanks to io stream manipulators.


sync_with_stdio
Syntax:
  #include <fstream>
  static bool sync_with_stdio( bool sync=true );

The sync_with_stdio() function allows you to turn on and off the ability for the C++ I/O system to work with the C I/O system.


tellg
Syntax:
  #include <fstream>
  pos_type tellg();

The tellg() function is used with input streams, and returns the current "get" position of the pointer in the stream.


tellp
Syntax:
  #include <fstream>
  pos_type tellp();

The tellp() function is used with output streams, and returns the current "put" position of the pointer in the stream.

For example, the following code displays the file pointer as it writes to a stream:

 string s("In Xanadu did Kubla Khan...");
 ofstream fout("output.txt");
 for( int i=0; i < s.length(); i++ ) {
   cout << "File pointer: " << fout.tellp();
   fout.put( s[i] );
   cout << " " << s[i] << endl;
 }
 fout.close();		
		


unsetf
Syntax:
  #include <fstream>
  void unsetf( fmtflags flags );

The function unsetf() uses flags to clear the io stream format flags associated with the current stream.


width
Syntax:
  #include <fstream>
  int width();
  int width( int w );

The function width() returns the current width, which is defined as the minimum number of characters to display with each output. The optional argument w can be used to set the width.

For example:

   cout.width( 5 );
   cout << "2";		
		

displays

       2		
		

(that's four spaces followed by a '2')


write
Syntax:
  #include <fstream>
  ostream& write( const char* buffer, streamsize num );

The write() function is used with output streams, and writes num bytes from buffer to the current output stream.