. Advertisement .
..3..
. Advertisement .
..4..
The getline() can help you read a file line by line in C++. Read in detail how to make use of this with the explanation and examples below.
Read A File Line By Line In C++ With getline()
The getline()
function from the C++ Standard Library can extract characters from an object until it runs into a delimiter and stores those characters into a string. This extraction ends when the function reaches the end of file or due to other errors during the operation.
All occurrences of the delimiter will be discarded and not get stored in the resulting string. The input operation starts again after it.
The syntax of the getline()
function:
istream& getline (istream& is, string& str, char delim);
Parameters:
- is: the istream object you want to extract characters from.
- str: the string object you want to store the extracted character. Keep in mind that if there is existing content in this object, it will be replaced by the extracted characters, which will be appended in the same manner as push_back.
- delim: the delimiting character you want to use to split and extract content from the istream object. The default value is ‘\n’ – the newline character.
However, since we want to read lines from a text file, an input stream is required first. You can achieve this with std::ifstream
– the input stream class in the standard library.
You can use objects of the ifstream class to create a filebuf object and carry out necessary input and output operations, which can apply to the text file associated with the object. There are two options for associating a file in your system with an ifstream object: the open()
method and the constructor.
You can declare an ifstream object first and use the open()
method to open the file you want to read. This associates the file with the ifstream object, allowing you to perform input and output operations on the content.
Syntax of the open()
method:
void open (const char* filename, ios_base::openmode mode = ios_base::in);
Parameters:
- filename: a string that contains the path and file name you want to read its content.
- mode: these optional flags describe the I/O mode you need to set for the file. They can be: in (input), out (output), binary, ate (at end), app (append), or trunc (truncate). You can combine them with the bitwise operator OR (|).
Even when you don’t set ta mode for an ifstream object, it will have the input flag, meaning the file is available for reading, and input operations are supported.
Suppose we have a text file sample.txt with the following content:
ITTutoria
Stack Overflow
Quora
Reddit
This example shows how you can combine the ifstream object with the getline()
function to read the above file line by line in C++:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string filepath = "/home/tutorials/cpp/sample.txt";
std::ifstream file (finepath, std::ifstream::in);
ifstream file;
file.open(filepath);
if (!file.is_open()) {
cout<<"Unable to open the file."<<endl;
return 0;
}
string line;
while (getline(file, line))
{
cout<<line<<endl;
}
file.close();
return 0;
}
Output:
ITTutoria
Stack Overflow
Quora
Reddit
You can also use the ifstream
constructor with the following syntax:
explicit ifstream (const char* filename, ios_base::openmode mode = ios_base::in);
This constructor uses the same parameters with the same meanings as the open()
method. We can rewrite a portion of the above program with this constructor like this:
string filepath = "/home/tutorials/cpp/sample.txt";
std::ifstream file (filepath, std::ifstream::in);
You will have the same ifstream object as before, and there will be no change in the result.
Conclusion
The getline()
can help you read a file line by line in C++ when you have converted the file into an input stream with the ifstream class. The result can be used for other operations. For instance, if you want to split this string into space, check out this guide.
Leave a comment