HomeLinuxRandom Entry Information in C++

Random Entry Information in C++


After we wish to have random entry to any file, both to open it up for studying functions or write the modifications in it, we might use the random entry file operate. This operate is the quickest ever methodology to randomly entry any random file which lacks the order fairly than these sequential ones the place the order has given precedence and therefore take a protracted processing time. One such instance is that if we wish to play any content material from the mp3 playlist, we are able to merely entry that specific mp3 file utilizing the random entry file operate as an alternative of going via all of the playlists as achieved within the sequential file entry methodology.

Syntax:
We’re a lot accustomed to the “iostream” header file/commonplace library which we add manually to the C++ supply file earlier than creating any mission. That is achieved to take the usual enter from the “Cin” operate and to put in writing the usual output utilizing the “Cout” methodology/operate from the library. To randomly entry the recordsdata in C++, we have now so as to add one other commonplace library which is acknowledged because the “fstream”. This library is additional divided into three extra knowledge varieties to supply the random entry file operate. These capabilities are mentioned one after the other as follows:

ofstream
ifstream
fstream

To carry out the processing of the recordsdata, each the “fstream” and the “iostream” are necessary to be added to the supply file of C++. Within the previously-mentioned knowledge varieties, the primary is the ofstream knowledge kind. That is the info kind that offers with the output file stream and is devoted to the creation of recent recordsdata and in addition to writing the info/info to both the newly created or beforehand present recordsdata.

The second knowledge kind is the ifstream. It’s utilized to learn the enter knowledge from the recordsdata because it supplies the illustration of the enter file stream.
The third one, which is the ultimate knowledge kind, is the fstream. This knowledge kind is the generic illustration of each earlier mentioned knowledge kinds of the recordsdata stream which merely implies that we might use such knowledge kind for writing, studying the info to the recordsdata, and in addition for creating new recordsdata.

With this detailed rationalization of every of the info varieties, let’s virtually use them within the file stream and check out the completely different examples of them.

Instance: Opening a File and Writing the Knowledge Into It Utilizing the “Fstream” Commonplace Library
This very instance depicts how we are able to open a file, add the specified info to it, after which learn that knowledge within the file utilizing the random entry recordsdata methodology. Earlier than we begin, we needs to be conscious that we have to both create that file or just use the already present file to open any file. At present, we open the already present file, write down the knowledge to it, after which show the contents within the file because the output.

Shifting on, we ensure that so as to add the required header recordsdata for the file operation because the “ < iotream> ” and the “ <fstream>”. As soon as these header recordsdata are added to the supply file, the subsequent step is to open the file in order that we are able to make the modifications to its content material. For that reason, we use the info varieties object, e.g. ofstream or fstream. Right here, we is not going to use the ifstream since it’s made to open the file when solely studying 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” knowledge kind. Then, we name the “open()” operate with this object which takes in two of the arguments – the “ios:: out | ios:: trunc”. The ios:: out permits to open the desired file for writing and the ios::trunc is to truncate the contents of the preexisting recordsdata.

The subsequent step in this system is to take the enter from the consumer to put in writing it into the file as a result of we opened a file at this level and all of the beforehand present content material within the file is truncated. To get the enter, we use the “get line()” operate from “Cin” which makes use of the identify and the scale of the char array the place the enter knowledge is saved as its enter arguments. Then, we write this enter knowledge to the file with the ofstream’s object “outfile” and the array which is called as “enter”. After this, we name the “ignore()” operate to eliminate any redundant characters from the file’s earlier “get enter” operate and shut the file.

To learn the enter info that we simply wrote to the file, we open the file as soon as once more. However this time, we name the open() operate with the “ifstream’s object” knowledge kind which, in our case, we created as “inputfile” to open the file. It is because we wish to open the file for simply studying goal this time. We will learn the file with the ifstream knowledge kind as follows:

ifstream inputfile;
inputfile.open(“file.dat)
cout<< “learn from the file” << endl;
inputfile >> enter;
cout << enter << endl;

After studying the file, we show the content material within the file on the output terminal utilizing “Cout” after which shut the file once more utilizing the “shut()” operate.

Following the earlier rationalization, file opening, and file studying strategies, we applied an instance whose code is hooked up as follows:

#embody <fstream>
#embody <iostream>
utilizing namespace std;
int primary () {
   char enter[100];
 // open a file in write mode.
   ofstream outputfile;
   outputfile.open(“file.dat”);
   cout << “Write to  file” << endl;
   cout << “Enter phrase: “;
   cin.getline(enter, 100);
// writing of enter knowledge into  file.
   outputfile << enter << endl;
   cout << “Enter quantity: “;
   cin >>enter;
   cin.ignore();
   //  writing enter info in file.
   outputfile << enter << endl;
   // shut file.
   outputfile.shut();
   // opening of file in the one studying mode.
   ifstream inputfile;
   inputfile.open(“file.dat”);
   cout << “learn the information  from this file” << endl;
   inputfile >> enter;
   cout << enter << endl;
  //as soon as once more studying the file and show it.
   inputfile >> enter;
   cout << enter << endl;
  // shut the opened file.
   inputfile.shut();
   return 0;
}

This instance reveals that we first open a file and write the enter info to it utilizing the “ofstream” datatype. Then, we open the file once more to learn its content material utilizing the “ifstream” knowledge kind.

Conclusion

The article throws gentle on the random entry file operate in C++. Within the article, we described the three completely different knowledge varieties that permit us to carry out the assorted operations akin to studying, writing, and opening any random entry file to save lots of our time from following the sequential order to entry the file.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments