File Handling in cpp

 

File Handling in C++

Introduction

  • All programs that we have written so far have taken input data from the keyboard and output the data to the screen.
  • These outputs are lost as soon as we exit the program.
  • If we want to store data permanently, we can package the data on secondary storage devices as files.
  • A stream is a general name given to a flow of data.
  • In C++ a stream is represented by an object of a particular class.
  • We’ve used the cin and cout stream objects.
  • Different streams are used to represent different kinds of data flow.
  • For example, the ifstream class represents data flow from input disk files.

Stream Class Hierarchy

  • The ios class is the main one of all the stream classes, and contains the majority of the features we need to operate C++ streams.
  • The extraction operator >> is a member of the istream class, and the insertion operator << is a member of the ostream class.
  • Both of these classes are derived from the ios class.
  • The cout object, representing the standard output stream, is a predefined object of the ostream_withassign class, which is derived from the ostream class.
  • Similarly, cin is an object of the istream_withassign class, which is derived from istream.
  • The istream class, which performs input-specific activities, contains such functions as get( ), getline( ) and read( ), and the ostream class, which handles output or insertion activities, contains put( ) and write( ).
  • Three classes — istream_withassign, ostream_withassign, and iostream_withassign—are inherited from istream, ostream, and iostream, respectively.
  • They add assignment operators to these classes.
  • The ios class also contains a pointer to the streambuf class, which contains the actual memory buffer into which data is read or written, and the low-level routines for handling this data.
  • The ios_base class represents general properties of a stream, such as whether it’s open for reading and whether it’s a binary or a text stream.
  • File Input Stream reads data from disk files to the program.
  • The ifstream class provides input operations on files.
  • File Output Stream writes data to the disk file from the program.
  • The ofstream class provides output operations on files.
  • The fstream class supports simultaneous input and output operations on files.


Binary vs Text (Character) Files

  • All files can be categorized into one of two file formats — binary or text.
  • The major difference between these two is that a text file contains textual information in the form of alphabets, digits and special characters or symbols.
  • On the other hand, a binary file contains bytes or a compiled version of a text file.
  • A text file stores data in the form of alphabets, digits and other special symbols by storing their ASCII values and are in a human readable format .For example, any file with a .txt, .c, etc extension.
  • Whereas, a binary file contains a sequence or a collection of bytes which are not in a human readable format . For example, files with .exe, .mp3, etc extension . It represents custom data.
  • A small error in a textual file can be recognized and eliminated when seen.
  • Whereas, a small error in a binary file corrupts the file and is not easy to detect.
  • Since the data is not human readable it also adds to the security of the content as one might not be able to get data if the structure is not known.

Difference between Character IO and Binary IO

Character IOBinary IO
Text file contains the textual information in the form of alphabets, digits and special characters or symbol.Binary Files contains bytes or compiled version of a text file.
It stores data in the form of alphabets, digits and other special symbols by storing their ASCII values.Binary file consists of a sequence or a collection of bytes.
They are in human readable format.They are not.
A small error in a textual file can be recognized and eliminated when seen.A small error in binary file corrupts the file and is not easy to detect.
It lacks security.It adds security.


Unformatted Input/Output

  • Unformatted I/O is the most basic form of I/O
  • Unformatted I/O transfers the internal binary representation of the data directly between memory and file
  • For example, cout, cin, put( ), get( ), getline( ), write( ) perform
  • unformatted I/O operations
  • eg:-
#include <iostream>
using namespace std;
int main()
{
    char ch[6] = "Hello";
    for (int i = 0; i < 6; i++)
    {
        cout.write(ch, i);
        cout << endl;
    }
    return 0;
}

Formatted Input/Output

Formatted input reads characters from the input file and converts them to internal form. It converts the internal binary representation of the data to ASCII characters which are written to the output file. In formatted I/O, numbers are stored on disk as a series of characters. 

There are 3 ways to display formatted input and output:

(1) Using manipulators
(2) Using ios class member functions and flags
(3) Using user-defined manipulators


File access Pointers and their manipulators

Each file has two associated pointers known as the file pointers . One of them is called the input pointer (or get pointer) and the other is called output pointer (or put pointer). The input pointer is used for reading the content of a given file location and the output pointer is used for writing to a given file location. Each time a input or output takes place , the appropriate pointer is automatically advanced.