Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use QTextStream::readLine function?

Tags:

c++

qt

I am trying to read values from a text file using the Qt code given below.

int ReadFromFile(QString fileName)
{
   QFile file(fileName);
   if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
      return 1;

   QTextStream in(&file);
   while (!in.atEnd())
   {
      QString line = in.readLine(1); //read one line at a time
      QStringList lstLine = line.split(",");
   }
   file.close();
   return 0;
}

The content of the text file is as follows:

1,0.173648178  
2,0.342020143  
3,0.5  
4,0.64278761  
5,0.766044443  
6,0.866025404  

However readLine always returns one character at a time but my intention is to read one line at a time and to split each line to get the individual comma seperated values.

Am I missing something basic here?

like image 437
Martin Avatar asked Oct 26 '25 17:10

Martin


1 Answers

Yes. You're passing 1 for the maxlen parameter, which means to limit the line length to only 1 character. Try it without supplying anything for maxlen.

like image 168
kenrogers Avatar answered Oct 28 '25 06:10

kenrogers