View Full Version : QTextStream

10th September 2012, 08:47
I read a text file with QTextStream.
1.If I read contents with readLine (),it is ok.
2.Now I want to know how many rows first before I process the contents,so I use readALL(),and split it with "\n",I find that some text can not be splitted with "\n",so I try "\r\n" or "\r",and find it is still not ok.and the number of the splitting strings is always 1.
How to do ?

10th September 2012, 09:47
Show the code you are using to split the string you get back from readAll()

10th September 2012, 10:14
QFile file( fileName );
if( !file.open( QIODevice::ReadOnly ) )
QMessageBox::warning(0,QObject::tr("Import SPS File"),QObject::tr("Can not open shot file:")+fileName);
return false;
QTextStream stream( &file );
QStringList allContents=stream.readAll().split("\n");

the elements number of allContents is always 1.

By the way,when the text file is edited by wordpad,and split the contents of it with"\n",it is ok.

10th September 2012, 10:40
So why can't you use readLine()?

10th September 2012, 11:49
I need to know how many rows of the text file before I read line by line.

10th September 2012, 16:09
I need to know how many rows of the text file before I read line by line.

And how is that different from reading the whole file at once using readAll()? Why can't you do this:

QStringList lines;
while(!file.atEnd()) lines << stream.readLine();
qDebug() << "I have" << lines.count() << "lines".