Wassim AZIRAR Wassim AZIRAR - 1 year ago 166
C++ Question

Read a text file line by line in 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))
{
...
}

Answer Source

Use this code:

QFile inputFile(fileName);
if (inputFile.open(QIODevice::ReadOnly))
{
   QTextStream in(&inputFile);
   while (!in.atEnd())
   {
      QString line = in.readLine();
      ...
   }
   inputFile.close();
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download