CIS 150 Text File I/O

Objectives

  • Open a text file for reading
  • Open a text file for writing
  • Open a text file for appending
  • Detect and handle errors when trying to open a file
  • Read data from a text file
  • Write data to a text file
  • Use iteration to read all the data in a text file
  • Close a file

Overview

  • Include the proper library: #include <fstream>
  • Usually also: #include <iostream>
  • Make sure the program can see what you want in that library: using namespace std;
  • More precise versions of above
    • using std::fstream;
    • using std::ifstream;
    • using std::ofstream;
  • The steps to processing a file:
    • open the file
    • process the file records
    • close the file
  • You get a reference to the file stream when you open it. That reference is then used to access the file stream, and eventually to close it.

Opening a text file

  • Note: Reading from a file is input, writing is output.
  • Declaring a file stream variable: fstream file;
  • Opening a text file named test.txt for reading: file.open("test.txt", ios::in);
  • Easier version of above: ifstream file("test.txt"); // for reading
  • Detecting an error opening the file: if (!file) { ... error code ... }
  • Opening a text file named test.txt for writing (over-writing): file.open("test.txt", ios::out);
  • Easier version of above: ofstream file("test.txt"); // for writing
  • Opening a text file named test.txt for appending (adding to end): file.open("test.txt", ios::app);
  • Easier version of above: ofstream file("test.txt", ios::app); // for writing

Reading or writing to a file stream

By now you should be familiar with using the insertion operator (<<) to write to an output stream such as cout. You use the insertion operator to write to your output file stream the same way.

You should also be familiar with using the extraction operator (>>) to read from an input stream such as cin. You use the extraction operator to read from your input file stream the same way.

You can also read entire lines including whitespace using the getline function, and ignore characters in the stream using the .ignore() function.

Examples:

  • ofile << num1 << " " << num2 << endl;
  • ifile >> num1 >> num2;
  • ifile.ignore();
  • ifile.ignore(1000, '\n');
  • getline(ifile, name);

Checking for EOF and closing file streams

  • Closing a file stream is easy. Just use its close() function: ifile.close();
  • EOF stands for end of file.
  • File streams have an eof() function you can check: ifile.eof();
  • You can also do an EOF check by testing the file stream object: while (ifile) { ... not EOF ... }

Example

This example is also available here: fileio1.cpp

#include <fstream> #include <iostream> using std::cout; using std::endl; using std::ifstream; using std::ofstream; int main(void) { int n; float x = 0.0f; // open output file ofstream ofile("testfileio1.txt"); if (!ofile) { cout << "Error: couldn't open output file" << endl; return 1; } // write out 10 records of two space separated fields for (n=1; n<10; n++, x+=25) { ofile << n << " " << x << endl; } ofile.close(); // open input file ifstream ifile("testfileio1.txt"); if (!ifile) { cout << "Error: couldn't open input file" << endl; return 2; } // use priming read to read all records in file ifile >> n >> x; while (ifile) { cout << n << ", " << x << endl; ifile >> n >> x; } ifile.close(); return 0; }