C++ Working With Files

Many real-life scenarios are there that handle a large number of data, and in such situations, you need to use some secondary storage to store the data. The data are stored in the secondary device using the concept of files. Files are the collection of related data stored in a particular area on the disk. Programs can be written to perform read and write operations on these files.

Working with files generally requires the following kinds of data communication methodologies:

  • Data transfer between console units
  • Data transfer between the program and the disk file

So far we have learned about iostream standard library which provides cin and cout methods for reading from standard input and writing to standard output respectively. In this chapter, you will get to know how files are handled using C++ program and what are the functions and syntax used to handle files in C++.

Here are the lists of standard file handling classes

  1. Ofstream: This file handling class in C++ signifies the output file stream and is applied to create files for writing information to files
  2. Ifstream: This file handling class in C++ signifies the input file stream and is applied for reading information from files
  3. Fstream: This file handling class in C++ signifies the file stream generally, and has the capabilities for representing both ofstream and ifstream

All the above three classes are derived from fstreambase and from the corresponding iostream class and they are designed specifically to manage disk files.

Opening and Closing a File in C++

If programmers want to use a disk file for storing data, they need to decide about the following things about the file and its intended use. These points that are to be noted are:

  • A name for the file
  • Data type and structure of the file
  • Purpose (reading, writing data)
  • Opening method
  • Closing the file (after use)

Files can be opened in two ways. They are:

  1. Using constructor function of the class
  2. Using member function open of the class

Opening a File

The first operation generally performed on an object of one of these classes to use a file is the procedure known as to opening a file. An open file is represented within a program by a stream and any input or output task performed on this stream will be applied to the physical file associated with it. The syntax of opening a file in C++ is:

open (filename, mode);

There are some mode flags used for file opening. These are:

  • ios::app: append mode
  • ios::ate: open a file in this mode for output and read/write controlling to the end of the file
  • ios::in: open file in this mode for reading
  • ios::out: open file in this mode for writing
  • ios::trunk: when any file already exists, its contents will be truncated before file opening

Closing a file in C++

When any C++ program terminates, it automatically flushes out all the streams releases all the allocated memory and closes all the opened files. But it is good to use the close() function to close the file related streams and it is a member of ifsream, ofstream and fstream objects.

The structure of using this function is:

void close();

General functions used for File handling

  1. open(): To create a file
  2. close(): To close an existing file
  3. get(): to read a single character from the file
  4. put(): to write a single character in the file
  5. read(): to read data from a file
  6. write(): to write data into a file

Reading from and writing to a File

While doing C++ program, programmers write information to a file from the program using the stream insertion operator (<<) and reads information using the stream extraction operator (>>). The only difference is that for files programmers need to use an ofstream or fstream object instead of the cout object and ifstream or fstream object instead of the cin object.

Example:
#include <iostream>
#include <fstream.h>

void main () {
  ofstream file;
  file.open ("egone.txt");
  file << "Writing to a file in C++....";
  file.close();
  getch();
}

Another Program for File Handling in C++

Example:
#include <iostream>
#include<fstream.h>

void main()
{
  char c,fn[10];
  cout<<"Enter the file name....:";
  cin>>fn;
  ifstream in(fn);
  if(!in)
  {
    cout<<"Error! File Does not Exist";
    getch();
    return;
  }
  cout<<endl<<endl;
  while(in.eof()==0)
  {
    in.get(c);
    cout<<c;
  }
  getch();
}

Another C++ Program to Print Hello GS to the Console

Example:
#include <iostream>
#include<fstream.h>
#include<math.h>

void main()
{
 ofstream fileo("Filethree");
 fileo<<"Hello GS";
 fileo.close();
 ifstream fin("Filethree");
 char ch;
 while(fin)
 {
  fin.get(ch);
  cout<<ch;
 }
 fin.close();
 getch();
}

Scroll Back to Top