C++: Read File Line by Line

Files are very important parts of the routine life of programmers, computer users, and students when working on any operating system. These files help us keep our data save in different formats with security and redundancy. Therefore, within this simple tutorial today, we will be discussing the C++ examples to read data from a file line by line in Ubuntu 20.04 system. Let’s start with opening a shell terminal in the Ubuntu 20.04 system by using the “Ctrl+Alt+t” shortcut key. The console will be released on your screen. After opening it, we need to install the c++ compiler named “g++” on your system as we are doing our code in the C++ language. For that, we will be using the apt package in our shell with the install command of the Ubuntu 20.04 system. The title “g++” will be used at the end of this command appended below. We have added our sudo password after executing the command to install and configure the g++ compiler.

It requires our confirmation within the installation to continue it. So, we have entered “y” and used the Enter key to continue.

As the g++ compiler is installed, we are ready to implement our examples now.

We have used the “touch” query in the command-line shell to create a new c++ file in our Ubuntu 20.04 system. The filename has been given as “read.cc”. This file could be opened in any editor to add c++ code to it. These editors include text, vim, and nano editor. We recommend using the nano editor as it can be opened in the terminal. So, we have been utilizing the “nano” command to open the file “read.cc” in the nano editor.

Example 01:

So, the blank file will be unwrapped in the GNU editor. We have started our code by including some important and necessary header files. The header files include input-output stream, string, and file stream header files. The code will be started without the namespace. So, we have started the main method() first. In our first example, we used the already created file to read text from it line by line. The “std” keyword will be utilized to use the standard input and outputs in the code.

So, the input file stream is used to read the file “new.txt” from the home directory using the file object, i.e., ReadF. The string type variable is declared name “data” to just simply save the text of the file into it after reading. Now, the while loop is being utilized here to use the getline() function of C++. This function takes two arguments, i.e., the “ReadF” object and string variable “data”. The getline() function will be reading the text from the new.txt file and saving it to the variable “data” until the file is not empty. While this condition is true, we used the “cout” statement to display the file data in the shell. Let’s save the code with “Ctrl+S” and use the “Ctrl+X” shortcut to exit the nano editor.

Let’s compile the newly implemented code in the terminal using the “g++” compiler. After that, execute the file with the “./a.out” instruction. The output shows the file data line by line on the terminal.

Example 02:

Our first example was all about reading the data from a file line by line and display on the shell. In this illustration, we will be writing the data into the file and then read line by line. So, we have updated the same code by opening the “read.cc” file. Added the headers and namespace. Within the main() method, we have used the output file stream object, i.e., “WriteF” to write data in the new.txt file. The user has added data into the file using the WriteF object. The WriteF object is taking a single line input from the user here. The WriteF object will be closed, and the “data” is declared. The input file stream object, i.e., ReadF, is used to read the text from the new.txt file line by line. While the file is not empty, it will continue to save the data in the variable “data” from the file object ReadF using the getline method. The cout statement displays the data line by line on the shell.

At last, the ReadF input file stream object has been closed. The program ends here. So, let’s execute it.

First, compile the updated code with the g++ compiler and then execute it. After the execution, we have got the one-line output as the user writes the 1 line in the new.txt file.

Example 03:

So, here comes our last but not the least example to read the data from a file line by line. So, we have opened the same read.cc file and updated its code as shown below. So, we have started with the inclusion of necessary header files, i.e., iostream, string, and fstream, for file handling. Then a namespace has been utilized before the start of the main function. This script is slightly diverse from both the two above example codes. We have declared the object “File” of header file “fstream” at the start of a main() function. This object will be used to open, write, read, and close the file. Firstly, we have used the object File to open the “new.txt” file. The standard “ios” package identifies the stream type, i.e., input or output.

You can see, we have specified it as an output stream. The “out” keyword will be used for writing in the file after opening it, while the “in” keyword will be used to read from the file. Thus, we have utilized the “if” statement to check the condition of the “File” object has opened the particular file or not. For this purpose, the “is_open” function has been utilized. If the file is void, the File stream object will input 5 lines in the file as shown. After that, the File object will close the output stream. Now, we have opened the same file new.txt with the Stream object “File” to via the “ios::in” declaration. The “if” statement has been utilized here to crisscross if the file is vacant or not. If so, then the string type variable “data” is declared. The getline() function within the while loop will get the data from the File object line by line and save it to the variable “data”. This variable “data” will be utilized to display the lines on the shell. In the end, the file object is closed.

After the compilation and running of this file, we have got the lines of new.txt file line by line on our screen, as shown below.

Conclusion:

We have done an extremely great job covering the topic C++: read file line by line in Ubuntu 20.04 system. We have started with this simple example of reading data from an already created text file. Afterwords, we have also seen how to use file handling to write data into a file, read it from it, and display it on the shell. We hope you will like it.



from https://ift.tt/3n09vk0

Post a Comment

0 Comments