CPP Program To Implement a File Handling Concept Using Sequential Access

Aim:

To implement a file handling concept using sequential access.

C++ File Handling

As we know, at the time of execution, every program comes in the main memory to execute. Main memory is volatile and the data would be lost once the program is terminated. If we need the same data again, we have to store the data in a file on the disk. A file is a sequential stream of bytes ending with an end-of-file marker.

Types of files supported by C++:

  • Text Files
  • Binary Files

Difference between text file and binary file

  • Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable.
  • A newline(\n) character is converted into the carriage return-linefeed combination before being written to the disk. In binary file, these conversions will not take place.
  • In text file, a special character, whose ASCII value is 26, is inserted after the last character in the file to mark the end of file. There is no such special character present in the binary mode files to mark the end of file.
  • In text file, the text and characters are stored one character per byte. For example, the integer value 23718 will occupy 2 bytes in memory but it will occupy 5 bytes in text file. In binary file, the integer value 23718 will occupy 2 bytes in memory as well as in file.

The fstream.h Header File

C++’s standard library called fstream, defines the following classes to support file handling.

  • ofstream class : Provides methods for writing data into file. Such as, open(), put(), write(), seekp(), tellp(), close(), etc.
  • ifstream class : Provides methods for reading data from file. Such as, open(),get(), read(), seekg(), tellg(), close(), etc.
  • fstream class : Provides methods for both writing and reading data from file. The fstream class includes all the methods of ifstream and ofstream class.

Opening a file using open() member function

The open() function takes file-name argument. The purpose of opening the file i.e, whether for reading or writing, depends on the object associated with open() function.

Example of opening file

ofstream fout; fout.open("filename"); // Open file for writing ifstream fin; fin.open("filename"); // Open file for reading fstream f; f.open("filename",mode); // Using fstream, we can do both read and write, therefore, // mode specifies the purpose for which the file is opened.
Code language: JavaScript (javascript)

File Opening Modes

ModePurpose
ios::inOpen a text file for reading.
ios::outOpen a text file for writing. If it doesn’t exist, it will be created.
ios::ateOpen a file and move the pointer at the end-of-file.
ios::appOpen a text file for appending. Data will be added at the end of the existing file. If a file doesn’t exist, it will be created.
ios::binaryOpen file in binary mode.
ios::nocreateThe file must already exist. If a file doesn’t exist, it will not create a new file.
ios::truncIf the file already exists, all the data will be lost.

Reading and Writing into File

We can read data from files and write data to file in four ways.

  • Reading or writing characters using get() and put() member functions.
  • Reading or writing formatted I/O using insertion operator ( << ) and extraction operator ( >> ).
  •  Reading or writing object using read() and write() member functions.

Algorithm:

  1. Start the process
  2. Get the input string
  3. Write the input string char by char into a file called “Text” using put() function
  4. Read the input string char by char from the file called “Text” using get() function
  5. Display the result

Program:

#include <fstream>
#include <iostream>
using namespace std;
int main () 
{
char data[100];
ofstream outfile;
outfile.open("afile.dat");
cout << "Writing to the file" << endl;
cout << "Enter your name: \n"; 
cin.getline(data, 100);
outfile << data << endl;
cout << "Enter your age: \n"; 
cin >> data;
cin.ignore();
outfile << data << endl;
outfile.close();
ifstream infile; 
infile.open("afile.dat"); 
cout << "Reading from the file" << endl; 
infile >> data;
cout << data << endl;
infile >> data; 
cout << data << endl; 
infile.close();
return 0;
}

Execution:

Input
AAAA 100


Output
Writing to the file
Enter your name: 
Enter your age: 
Reading from the file
AAAA
100

Result:

Thus, the implement a file handling concept using sequential access was executed successfully.

Leave a Comment