File I/O

Requires #include <fstream> ; preprocessor directive.

 

The ofstream can be used for output to create files and write data to them.

 

The ifstream can be used for input to read data from files.

 

A file must be opened before it can be read from or written to.

 

File output using the ofstream class:

#include <iostream>
#include <fstream>
using namespace std;

int main () {

	ofstream myFile("testFile.dat") ;

	if (!myFile){
		cerr << "Couldn't open file for writing." ;
		return 1 ;
	}

	myFile << "Here's the first line of text!" << endl ;

	myFile << "Here's another line of text!" << endl ;

	return 0;
}

Compile & Run will create the specified file 'testFile.dat' that contains the following two output lines:

Here's the first line of text!
Here's another line of text!

 

 

File input using the ifstream class to open and read the above created file:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {

	ifstream myFile("testFile.dat") ;

	if (!myFile){
		cerr << "Couldn't open file for reading." ;
		return 1 ;
	}

	while (myFile) {

		string fileData ;
		getline(myFile, fileData) ;
		cout << fileData << endl ;
	}

	return 0;
}

Compile & Run:

Here's the first line of text!
Here's another line of text!

 

 

File Modes

Ios file mode Meaning
app Opens the file in append mode
ate Seeks to the end of the file before reading/writing
binary Opens the file in binary mode (instead of text mode)
in Opens the file in read mode (default for ifstream)
nocreate Opens the file only if it already exists
noreplace Opens the file only if it does not already exist
out Opens the file in write mode (default for ofstream)
trunc Erases the file if it already exists

 

 

Append text to a file using the ios:: app to append the text to the end of the file:

#include <iostream>
#include <fstream>
using namespace std;

int main () {

	ofstream myFile("testFile.dat", ios:: app) ;

	if (!myFile){
		cerr << "Couldn't open file for writing." ;
		return 1 ;
	}

	myFile << "Wow, yet another line of amazing text" << endl ;

	myFile << "Even more text..." << endl ;

	return 0;
}

Compile & Run will append the specified text to 'testFile.dat' that now contains the following four output lines:

Here's the first line of text!
Here's another line of text!
Wow, yet another line of amazing text
Even more text...

 

 

Closing and reopening files:

#include <iostream>
#include <fstream>
using namespace std;

int main () {

	ofstream myFile("testFile.dat", ios:: app) ;

	if (!myFile){
		cerr << "Couldn't open file for writing." ;
		return 1 ;
	}

	myFile << "This is the 5th line of text" << endl ;

	myFile << "How much more text can we take!" << endl ;

	myFile.close() ;

	myFile.open("testFile.dat", ios::app) ;

	myFile << "OK this really is the last line of text" << endl ;

	return 0;
}

Compile & Run will append the specified text to 'testFile.dat' that now contains the following output lines:

Here's the first line of text!
Here's another line of text!
Wow, yet another line of amazing text
Even more text...
This is the 5th line of text
How much more text can we take!
OK this really is the last line of text

 

 

Stream state member functions in ios

  • good() - checks whether the stream is ready for input/output operations
  • bad() - returns true if reading / writing operations fail
  • fail() - returns true, in a similar manner as bad(), but more specifically upon formatting errors (e.g. when an alphabetical character is extracted instead of a number)
  • eof() - returns true if opened file is at the end
  • good() - returns false, if any of the above would return true
  • clear() ¬†- takes no parameters, and is used reset the state flags
  • clear(state) ¬†- clears all flags and sets the state flag passed in
  • rdstate() - returns the currently set flags
  • setstate(state) - sets the state flag passed in

Leave a Reply