Files and I/O operations in C++ programming



Definition of files and I/O operations

A file is a collection of information, usually stored on a computer’s disk. Information can be saved to files and later retrieved. Files are crucial to the operation of many real-world programs.

Examples of familiar types of software packages that use files extensively are:

  • Word Processors: Word processing programs are used to write letters, memos, reports, and other documents. The documents are then saved in files so they can be edited and printed.
  • Database Management Systems: DBMSs are used to create and maintain databases. Databases are files that contain large collections of information, such as payroll records, inventories, sales statistics, and customer records.
  • Spreadsheets: Spreadsheet programs are used to work with numerical data. Numbers and mathematical formulas can be inserted into the rows and columns of the spreadsheet. The spreadsheet can then be saved to a file for use later.
  • Compilers: Compilers translate the source code of a program, which is saved in a file, into an executable file. Throughout the previous chapters of this book you have created many C++ source files and compiled them to executable files.

The name of a file being given to a program can be simple, consisting of just the name of the file, as in myfile.dat or it can be a full pathname like C:\csc\programs\myfile.dat which specifies the path through the directories on the system that the computer should follow to locate the file.

Using the fstream object

File stream objects require the programmer to specify whether the object will be used for input, output, or both. This is done through the use of file open modes.

A file open mode is an option, or flag, that the programmer can set to determine how the file will be used. The open member function of the file stream objects is overloaded to take a file mode as an optional second parameter.

File open modes are predefined values that are members of the ios class. The ios::in mode is used to set the fstream object for input, and ios::out is used to set it for output.

For example, we can open a file input.dat for input, and output.dat for output, using the fstream object and the appropriate file modes as follows:


    fstream inFile, outFile;
    inFile.open("input.dat", ios::in);
    outFile.open("output.dat", ios::out);

It is possible to combine several file open mode flags when a file is being opened. The combination of flags is achieved through the bitwise or operator |. The bitwise or operator, which occupies the same key as the backslash on most keyboards.

Reading and Writing Files

File stream objects have a lot of the same member functions as, and behave similarly to, the iostream objects cin and cout. In fact, even the I/O manipulators work with the fstream objects.

In particular, the insertion operator <<, the extraction operator >>, many member functions such as getline, and even the library function getline for working with string objects work the same way with file stream objects as they do with the iostream objects.

Program below shows the use of the insertion operator and of the getline function with an fstream object.

It opens a file for writing, writes two sentences to it, closes the file, and then opens the file for reading. The contents of the file are then printed on the screen.


    #include <iostream>
    #include <fstream>
    using namespace std;
    int main() {
    fstream dataFile; // file object
    string buffer; // Used to read line from file
    
    // Create a new file named myfile.dat to write to
    dataFile.open("myfile.dat", ios::out);
    
    // Write two lines to the file
    dataFile << "Now is the time for all good men" << endl
             << "to come to the aid of their country.";
    
    // Close the file
    dataFile.close();
    
    // Open the file for input
    dataFile.open("myfile.dat", ios::in);
    
    // Read a line into a buffer and print the line
    getline(dataFile, buffer);
    cout << buffer << endl;
    
    // Read a second line and print it
    getline(dataFile, buffer);
    cout << buffer << endl;
    
    // Close the file
    dataFile.close();
    }

    Program Output :
    Now is the time for all good men
    to come to the aid of their country.

There are other file modes besides ios::in and ios::out. Although we have illustrated file modes for fstream objects, file modes can be used with ifstream and ofstream objects as well. Of course, some file modes do not make sense with certain file stream objects:

For example, you should not use the ios::in mode with an ofstream object. Certain file streams are opened with some of the modes already set by default.


Ads Right