| by Arround The Web | No comments

Random Access Files in C++

When we want to have random access to any file, either to open it up for reading purposes or write the changes in it, we may use the random access file function. This function is the fastest ever method to randomly access any random file which lacks the order rather than those sequential ones where the order has given priority and hence take a long processing time. One such example is that if we want to play any content from the mp3 playlist, we can simply access that particular mp3 file using the random access file function instead of going through all the playlists as done in the sequential file access method.

We are much familiar with the “iostream” header file/standard library which we add manually to the C++ source file before creating any project. This is done to take the standard input from the “Cin” function and to write the standard output using the “Cout” method/function from the library. To randomly access the files in C++, we have to add another standard library which is recognized as the “fstream”. This library is further divided into three more data types to provide the random access file function. These functions are discussed one by one as follows:


To perform the processing of the files, both the “fstream” and the “iostream” are mandatory to be added to the source file of C++. In the previously-mentioned data types, the first is the ofstream data type. This is the data type that deals with the output file stream and is dedicated to the creation of new files and also to writing the data/information to either the newly created or previously existing files.

The second data type is the ifstream. It is utilized to read the input data from the files as it provides the representation of the input file stream.
The third one, which is the final data type, is the fstream. This data type is the generic representation of both earlier discussed data types of the files stream which simply means that we may use such data type for writing, reading the data to the files, and also for creating new files.

With this detailed explanation of each of the data types, let’s practically use them in the file stream and try out the different examples of them.

Example: Opening a File and Writing the Data Into It Using the “Fstream” Standard Library
This very example depicts how we can open a file, add the desired information to it, and then read that data in the file using the random access files method. Before we start, we should be aware that we need to either create that file or simply use the already existing file to open any file. Currently, we open the already existing file, write down the information to it, and then display the contents in the file as the output.

Moving on, we make sure to add the necessary header files for the file operation as the “ < iotream> ” and the “ <fstream>”. Once these header files are added to the source file, the next step is to open the file so that we can make the changes to its content. For this reason, we use the data types object, e.g. ofstream or fstream. Here, we will not use the ifstream since it is made to open the file when only reading is required. The syntax to open the file is as follows:

ofstream outfile;
outputfile.open(“file.dat”, ios :: out | ios :: trunc );

To open up the file, we create an object of the “ofstream” data type. Then, we call the “open()” function with this object which takes in two of the arguments – the “ios:: out | ios:: trunc”. The ios:: out allows to open the specified file for writing and the ios::trunc is to truncate the contents of the preexisting files.

The next step in the program is to take the input from the user to write it into the file because we opened a file at this point and all the previously existing content in the file is truncated. To get the input, we use the “get line()” function from “Cin” which uses the name and the size of the char array where the input data is stored as its input arguments. Then, we write this input data to the file with the ofstream’s object “outfile” and the array which is named as “input”. After this, we call the “ignore()” function to get rid of any redundant characters from the file’s previous “get input” function and close the file.

To read the input information that we just wrote to the file, we open the file once again. But this time, we call the open() function with the “ifstream’s object” data type which, in our case, we created as “inputfile” to open the file. This is because we want to open the file for just reading purpose this time. We can read the file with the ifstream data type as follows:

ifstream inputfile;
cout<< “read from the file” << endl;
inputfile >> input;
cout << input << endl;

After reading the file, we display the content in the file on the output terminal using “Cout” and then close the file again using the “close()” function.

Following the previous explanation, file opening, and file reading methods, we implemented an example whose code is attached as follows:

#include <fstream>
#include <iostream>
using namespace std;
int main () {
   char input[100];
 // open a file in write mode.
   ofstream outputfile;
   cout << "Write to  file" << endl;
   cout << "Enter word: ";
   cin.getline(input, 100);
// writing of input data into  file.
   outputfile << input << endl;
   cout << "Enter number: ";
   cin >>input;
   //  writing input information in file.
   outputfile << input << endl;
   // close file.
   // opening of file in the only reading mode.
   ifstream inputfile;
   cout << "read the info  from this file" << endl;
   inputfile >> input;
   cout << input << endl;
  //once again reading the file and display it.
   inputfile >> input;
   cout << input << endl;
  // close the opened file.
   return 0;

This example shows that we first open a file and write the input information to it using the “ofstream” datatype. Then, we open the file again to read its content using the “ifstream” data type.


The article throws light on the random access file function in C++. In the article, we described the three different data types that allow us to perform the various operations such as reading, writing, and opening any random access file to save our time from following the sequential order to access the file.

Share Button

Source: linuxhint.com

Leave a Reply