Reading From a File C++ Line by Line
Files are very important parts of the routine life of programmers, reckoner users, and students when working on any operating arrangement. These files help u.s.a. go on our data save in different formats with security and redundancy. Therefore, within this simple tutorial today, nosotros will be discussing the C++ examples to read data from a file line past line in Ubuntu 20.04 arrangement. Let'south offset with opening a shell terminal in the Ubuntu 20.04 system by using the "Ctrl+Alt+t" shortcut key. The panel will be released on your screen. Later opening it, we need to install the c++ compiler named "g++" on your system as nosotros are doing our lawmaking in the C++ language. For that, we will be using the apt bundle in our vanquish with the install command of the Ubuntu twenty.04 system. The title "g++" will be used at the end of this command appended beneath. 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, nosotros take entered "y" and used the Enter key to go along.
As the thou++ compiler is installed, we are ready to implement our examples now.
We have used the "touch on" query in the command-line shell to create a new c++ file in our Ubuntu 20.04 organisation. The filename has been given as "read.cc". This file could exist opened in any editor to add c++ code to it. These editors include text, vim, and nano editor. Nosotros recommend using the nano editor equally it tin can exist opened in the concluding. Then, we have been utilizing the "nano" control to open up 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 of import 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. And so, nosotros take started the master method() first. In our starting time instance, we used the already created file to read text from it line by line. The "std" keyword will be utilized to employ the standard input and outputs in the code.
So, the input file stream is used to read the file "new.txt" from the abode directory using the file object, i.eastward., ReadF. The string type variable is alleged name "data" to only simply relieve the text of the file into it after reading. At present, the while loop is beingness utilized here to use the getline() role 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 information technology to the variable "data" until the file is not empty. While this condition is true, we used the "cout" statement to brandish the file information in the shell. Let's save the code with "Ctrl+South" and use the "Ctrl+X" shortcut to get out the nano editor.
Permit's compile the newly implemented code in the terminal using the "1000++" compiler. After that, execute the file with the "./a.out" pedagogy. The output shows the file information line by line on the terminal.
Example 02:
Our outset instance was all most reading the data from a file line past line and display on the vanquish. In this analogy, nosotros will be writing the data into the file and and then read line past line. So, nosotros accept updated the same lawmaking by opening the "read.cc" file. Added the headers and namespace. Inside the principal() method, we have used the output file stream object, i.east., "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 airtight, and the "information" 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 volition go along to salve the information 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 airtight. The program ends here. So, permit'due south execute information technology.
Commencement, compile the updated code with the g++ compiler and and then execute it. Later the execution, nosotros take got the one-line output as the user writes the ane line in the new.txt file.
Example 03:
So, here comes our last only not the least example to read the information from a file line by line. Then, we have opened the same read.cc file and updated its code as shown below. And so, we take 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 starting time of the main function. This script is slightly various from both the 2 above case codes. We have declared the object "File" of header file "fstream" at the get-go of a main() function. This object will exist used to open, write, read, and shut the file. Firstly, we have used the object File to open the "new.txt" file. The standard "ios" packet identifies the stream type, i.due east., input or output.
Y'all tin see, nosotros have specified it as an output stream. The "out" keyword will exist used for writing in the file after opening it, while the "in" keyword will exist used to read from the file. Thus, we have utilized the "if" argument to cheque the condition of the "File" object has opened the detail file or not. For this purpose, the "is_open" office has been utilized. If the file is void, the File stream object volition input five lines in the file as shown. Afterwards that, the File object will close the output stream. Now, nosotros have opened the same file new.txt with the Stream object "File" to via the "ios::in" announcement. The "if" argument has been utilized here to crisscross if the file is vacant or not. If so, and then the string type variable "information" is declared. The getline() part within the while loop will become 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 cease, 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, every bit shown below.
Conclusion:
Nosotros have washed an extremely great job covering the topic C++: read file line by line in Ubuntu twenty.04 arrangement. We accept started with this simple example of reading data from an already created text file. Afterwords, we accept also seen how to use file treatment to write data into a file, read it from it, and display it on the shell. We hope you will like it.
Source: https://linuxhint.com/read-file-cpp/
0 Response to "Reading From a File C++ Line by Line"
Postar um comentário