How to Read Input From File Line by Line C++

Files are very important parts of the routine life of programmers, computer users, and students when working on any operating arrangement. These files help us keep our data relieve in different formats with security and redundancy. Therefore, inside this elementary tutorial today, we will be discussing the C++ examples to read data from a file line by line in Ubuntu twenty.04 system. Let'due south 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. Subsequently opening it, we demand to install the c++ compiler named "g++" on your system equally we are doing our code in the C++ language. For that, nosotros 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 stop of this control appended below. We have added our sudo password after executing the control to install and configure the chiliad++ compiler.

It requires our confirmation inside the installation to proceed it. So, we have entered "y" and used the Enter key to proceed.

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

We have used the "impact" query in the command-line crush to create a new c++ file in our Ubuntu 20.04 organization. The filename has been given equally "read.cc". This file could be opened in whatever 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, nosotros have been utilizing the "nano" command to open the file "read.cc" in the nano editor.

Instance 01:

So, the bare file volition be unwrapped in the GNU editor. Nosotros 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 lawmaking will be started without the namespace. So, we have started the main method() get-go. In our first instance, nosotros used the already created file to read text from it line past line. The "std" keyword will be utilized to employ the standard input and outputs in the code.

And then, the input file stream is used to read the file "new.txt" from the domicile directory using the file object, i.e., ReadF. The string type variable is alleged proper noun "data" to merely simply save the text of the file into it after reading. Now, the while loop is beingness utilized here to use the getline() function of C++. This function takes two arguments, i.eastward., 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 "information" until the file is not empty. While this status is true, we used the "cout" statement to display the file data in the shell. Allow'southward save the code with "Ctrl+S" and apply the "Ctrl+X" shortcut to exit the nano editor.

Allow's compile the newly implemented code in the terminal using the "thou++" compiler. After that, execute the file with the "./a.out" instruction. The output shows the file data line past line on the terminal.

Instance 02:

Our first case was all nigh reading the information from a file line past line and brandish on the crush. In this illustration, we volition be writing the data into the file and so read line by line. And then, we accept updated the aforementioned code by opening the "read.cc" file. Added the headers and namespace. Within the main() method, we accept 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 alleged. The input file stream object, i.due east., ReadF, is used to read the text from the new.txt file line past line. While the file is not empty, it will continue to salve the data in the variable "information" from the file object ReadF using the getline method. The cout argument displays the information line by line on the crush.

At final, the ReadF input file stream object has been airtight. The program ends here. So, let's execute information technology.

First, compile the updated code with the m++ compiler and and so execute it. Afterward the execution, nosotros have got the ane-line output as the user writes the 1 line in the new.txt file.

Example 03:

So, here comes our concluding only not the to the lowest degree instance to read the data from a file line past line. So, we have opened the aforementioned read.cc file and updated its code as shown below. And so, we have started with the inclusion of necessary header files, i.due east., iostream, cord, and fstream, for file handling. Then a namespace has been utilized earlier the start of the main function. This script is slightly diverse from both the two higher up example codes. We have declared the object "File" of header file "fstream" at the outset of a master() function. This object will be 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" package identifies the stream type, i.east., input or output.

You can run into, we take 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 exist used to read from the file. Thus, nosotros accept utilized the "if" statement to cheque the condition of the "File" object has opened the particular file or non. 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 take opened the same file new.txt with the Stream object "File" to via the "ios::in" declaration. The "if" statement has been utilized hither to crisscross if the file is vacant or non. If and so, then the string type variable "data" is alleged. The getline() part within the while loop volition get the information from the File object line past line and salvage it to the variable "data". This variable "information" volition 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, nosotros accept got the lines of new.txt file line past line on our screen, equally shown beneath.

Conclusion:

We have done an extremely corking task covering the topic C++: read file line by line in Ubuntu twenty.04 arrangement. Nosotros have started with this unproblematic example of reading data from an already created text file. Afterwords, we take also seen how to utilize file handling to write data into a file, read it from it, and brandish information technology on the beat out. Nosotros hope you volition like it.

Nigh the author

I am a cocky-motivated information applied science professional with a passion for writing. I am a technical writer and dear to write for all Linux flavors and Windows.

smithwilcorts95.blogspot.com

Source: https://linuxhint.com/read-file-cpp/

0 Response to "How to Read Input From File Line by Line C++"

إرسال تعليق

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel