Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a text file line by line in Qt

Tags:

c++

qt

How can I read a text file line by line in Qt?

I'm looking for the Qt equivalent to:

std::ifstream infile; std::string line; while (std::getline(infile, line)) {    ... } 
like image 258
Wassim AZIRAR Avatar asked Mar 26 '11 20:03

Wassim AZIRAR


People also ask

How do I read a text file line by line in Qt?

std::ifstream infile; std::string line; while (std::getline(infile, line)) { ... }

How do you create a text file in Qt?

Your current working folder is set by Qt Creator. Go to Projects >> Your selected build >> Press the 'Run' button (next to 'Build) and you will see what it is on this page which of course you can change as well. Show activity on this post. It can happen that the cause is not that you don't find the right directory.


1 Answers

Use this code:

QFile inputFile(fileName); if (inputFile.open(QIODevice::ReadOnly)) {    QTextStream in(&inputFile);    while (!in.atEnd())    {       QString line = in.readLine();       ...    }    inputFile.close(); } 
like image 119
Sergio Avatar answered Sep 30 '22 01:09

Sergio