In C++, reading a file line-by-line is a basic yet essential task. We can read a file line-by-line using the ifstream provided by the standard library through the std::getline() easily. Ifstream stands for input file stream, which provides the facility to read contents line-by-line.
In this article, we will discuss all the methods to read a file line-by-line using ifstream in C++.
Table of Contents:
Methods to Read a File Line-by-Line in C++
There are many ways to read a file line by line using ifstream. Below are the most common methods:
Method 1: Using std::getline() to Read a File Line by Line in C++
C++ provides the ifstream class by which files can be read. It is the most common method for reading a file line by line, using the std::getline() function, ensuring smooth extraction of text lines while handling whitespaces correctly.
Parameters:
- is: Input stream object from which the input is read.
- str: Stores the extracted line from the input stream.
- delim: Optional delimiter character that determines when to stop reading (default: \n).
Example:
File name: sample.txt
Hello!
Welcome to Intellipaat!
Let’s read a file!
Output:
In this code, at first, the attached file is opened, and the getline() reads one line at a time from the file. The while loop checks that each line has been read till the end and prints the output. Then, the file is closed.
Method 2: Using a While loop with eof() to Read a File Line by Line in C++
Since the most preferred method is to use the getline() function to read a file line-by-line in C++, we can also use the eof() function to read an entire file till the end by using it with a while loop. This function reads files line-by-line till the end of the file.
Example:
File name: sample.txt
Let’s read a file.
Reading a file line-by-line is an important task.
Output:
In this code, firstly, the attached file is opened, and getline() is used with the eof() function to read the file line-by-line till the end. The while loop continues till the end and prints the output. Then, the file is closed.
Method 3: Using std::istream::operator>> to Read a File Line by Line in C++
In C++, the std::istream::operator>> is used to extract the texts or data from an input stream and store it in variables. It works well for the basic data types, handles the whitespace automatically, and gives the output, which allows sequential and multiple parsing.
Example:
File name: sample.txt
Welcome to Intellipaat!
We are using std::istream::operator>> in C++ to read the file.
Output:
In this code, a file is opened and read by using the std::istream::operator<< very easily. The while loop checks that the file has been read till the end and prints the output. Then, the file is closed.
Method 4: Using str::istream::readsome() to Read a File Line by Line in C++
In C++, the str::istream::readsome() reads the input stream up to n characters and stores them in an array pointed with s. It reads the raw data provided into the buffer and stops as soon as the internal buffer runs out of characters. It does not handle delimiters or whitespaces and also does not format the data in the file.
Parameters:
- s: A pointer to the buffer where the data will be stored.
- n: The maximum number of characters to read from the input stream.
Example:
File name: sample.txt
Hello, this is test file.
Happy Coding!
Output:
In this code, the attached file is opened and read by using the std::istream::readsome() function. The while loop ensures that the file has been read till the end and prints the output. Then, the file is closed.
Method 5: Using std::ifstream::get() to Read a File Line by Line in C++
In C++, for reading files, we can also use the std::ifstream::get() function, as it is a low-level function and is suitable for basic character-by-character reading. It reads a single character from the input stream and stores it in a variable ch. Also, it has two versions; in another version, it reads n-1 characters in the character array and stops when it encounters a new line.
Parameters:
- ch: A character variable to store the read character.
- s: A character array where the data will be stored.
- n: It represents the number of characters to read.
Example:
File name: sample.txt
Hello, this is a test file.
Happy Coding!
Output:
In this code, std::ifstream::get() reads the file character-by-character, but it gives the output as a line. Then, the file is closed.
Method 6: Using std::ifstream::read() for Binary Files in C++
To read the raw binary data efficiently in C++, you can use std::ifstream with the read() function. You can use this method when dealing with non-text data such as images, videos, and audio files. It is suitable for both large and small files, as it provides reading files as a whole or in chunks.
Example:
File name: sample.bin
Hello binary file
Output:
In this code, a binary file is attached and opened. To read the binary files, we use the std::istream::read() function till the end. It can print output as a whole line or in chunks. Then, the file is closed.
Conclusion
Reading a file line-by-line in C++ is an essential part of processing data, and it can be done efficiently by using the std::getline(), std::istream(), or std::ifstream::read(). The other methods, such as eof(), operator(>>), and readsome(), can be used according to our needs because they provide some extra-level controls to the users. So, we can choose the methods based on our needs to read the file efficiently.