QFile is a class in the Qt library used for handling input/output operations with files. The readLine() function is a method of the QFile class that reads one line of text at a time from a file.
Example 1: Reading lines from a text file
#include #include
int main() { QFile file("file.txt"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return 1;
QTextStream in(&file); while (!in.atEnd()) { QString line = in.readLine(); // do something with the line }
file.close(); return 0; }
In this example, we are using QFile and QTextStream to read lines from a text file named "file.txt". The file is opened in read-only mode and in text mode. We create a QTextStream object to read data from the file, and then use a while loop to read and process each line of the file until we reach the end.
Package library: Qt
Example 2: Reading lines from a CSV file and splitting them into fields
#include #include
int main() { QFile file("data.csv"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return 1;
QTextStream in(&file); while (!in.atEnd()) { QString line = in.readLine(); QStringList fields = line.split(','); // do something with the fields }
file.close(); return 0; }
In this example, we are reading lines from a CSV file named "data.csv". We split each line into a list of fields using the split() method of the QString class, which takes a delimiter (in this case, a comma) as an argument. We can then manipulate or process the fields as needed.
Package library: Qt
C++ (Cpp) QFile::readLine - 30 examples found. These are the top rated real world C++ (Cpp) examples of QFile::readLine extracted from open source projects. You can rate examples to help us improve the quality of examples.