C++

Random Access Files in C++

Implementing the code of a random file in C++ is not an easy task, especially on the Linux operating system, but it can be done if you have file processing functions. This article will use a simple file structure supported by the C++ programming language by using source codes in the Linux operating system.

This idea will be explained using the Linux operating system; thus, you must have Ubuntu installed and configured on your PC. So, after you’ve downloaded and installed Virtual Box, you’ll need to configure it. Now you’ll need to add the Ubuntu file to it. You can go to Ubuntu’s official website and download the appropriate file for your machine and operating system. It will take several hours to install, and then you must setup it on the virtual system.

We utilized Ubuntu 20.04, but you can use the most recent version. You will need to have a text editor and access to a Linux console to complete the implementation, as we will be able to see the result of the source codes on the terminal via the query.

Random File access

We create an application to access information about files randomly. In a file, we access the information, and random access provides the user to fetch the record instantly, and it is done in any order. Random access also provides usability to locate the data immediately. This phenomenon is useful in many aspects of our daily life. For instance, in banking, reservation systems, this concept is used to fetch the record timely. C++ programming language is not involved in imposing any structure on a file. So the random access is then supposed to start from scratch. Many techniques are used for this purpose, but the simplest is to use the record having a fixed length.

In C++, the file system can use three classes that are present in the stream header file.

  • ofstream: It is a class of streams that makes us write on the files.
  • Ifstream: It is used when the user wants to read the data from the file only.
  • Fstream is used for both input and output streams to and from the file.

Now we are going towards some examples to explain the random access concept.

Example

This example deals with opening the file and adding data to it. After addition, the data is displayed as an output on the terminal. The file opening depends on two situations. One is opening the already existing file and writing data in it. Whereas another condition is creating a new file to add the record in it. First, we will explain the situation in which an already existing file is edited by adding the data. Two libraries of “iostream” and “fstream” are used.

# include <fstream>

In the main program, we create objects of “ofstream” out. This object is used to open the file.

# fout.open("file.txt")

File.txt” is an already created file. This file will be opened. We have used the file with the data, so according to the situation, our C++ program is designed to delete the already present data in the file, and then the new data is added successfully. Here a while loop is used to ensure the opening of the file. One thing that should be mentioned here is that as our file contains previous data, there is a need to display the file first before writing the new data through the terminal.

These 4 lines are already present. But these will be deleted when the new record is entered. Now coming back to the source code.

When the file is executed, the user is prompted to enter its data. As the data is entered in the terminal, that record is also added to the file.

# Getline( cin, line);

When the program is executed, the user will keep adding the data. To terminate or stop entering the record, one needs to have such a condition to stop the loop. So we use an if-statement here. That checks if the user enters the key “q” which means to quit, then the system stops adding data further.

If ( line == "q")

Break;

The ‘break’ statement is used to stop more execution. As we have described, the data from the terminal is added to the file; this is done by the object of the fstream we created.

# fout<<line <<endl;

After writing the data in the file, we will close it by using the same object. Till now, we have used the object of “ofstream” to write in the file. To read the data from the file, we need to create an object of ‘ifstream’, and that is fine.

# ifstream fin;

After creating the object, we will now open the file by providing the name of the file.

Fin.open("file.txt")

We have used a while loop to write the data; similarly, we need a while loop to read the data from the file until the end of the terminal. This time the record is fetched from the file to the console terminal. Then close the file through the object.

# fin.close();

After closing the file, go to the terminal and use the G++ compiler to compile the code.

$ g++ -o random random.c

$./ random

Random. c is the name of the file where we have written the C++ code. When we execute the file, you can see that new data is typed by the user. When the data to be entered is completed, the user needs to use ‘q’ to quit. As it is displayed in the below-cited image, press q.

Now when the user presses q, the data will be stopped entering the file, and then the control comes to “ifstream” for reading the data from the file. Now the file is closed. On ‘q’ the file will be opened to display the entered data so that data will be displayed again after showing the keyword ‘q’.

Now we go to the file manager and see the file. The data is entered, and the previous one is removed.

On the other hand, if we don’t have any file and use a random name, a new file will be created with that name.

For example, here, the file name is used ‘sample.txt’. Instead of ‘file.txt’. you can see that it is automatically created in the folder. Upon opening it, it displays the same text that you entered.

Random accessing of file through seekg() and seekp()

In both these functions, in seekg, ‘g’ stands for ‘GET’, and in seekp, ‘p’ stands for ‘PUT’. It contains two parameters. One is used to determine the number of bytes that should move the file pointer in the file.

Conclusion

This article is written upon the random access of the file in C++. The operating system we used is the Linux operating system. All examples used here are explained easily to remove the ambiguity from the user’s mind regarding input and the output streams. We hope that this struggle will be helpful in future perspectives.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.