PDA

View Full Version : Lost line with QProcess



starrl
18th May 2006, 04:34
I am having problems with the following short program I wrote with Qt 4.1.2


#include <QString>
#include <iostream>
#include <QProcess>

using namespace std;

void ProcessLine( QString );

int main()
{
QProcess *gzip = new QProcess;
QString line;
gzip->start("gunzip in.txt.gz -c");

while (gzip->waitForReadyRead()) {
while (gzip->canReadLine()) {
line = gzip->readLine();
ProcessLine( line );
}
}
}

void ProcessLine( QString line ) {
cout << line.toStdString();
}

my test file in.txt.gz contains the following all zipped
this is a test 1
this is a test 2
this is a test 3
this is a test 4
this is a test 5
this is a test 6
this is a test 7
this is a test 8

when I compile and run my code I get the following output.
this is a test 2
this is a test 3
this is a test 4
this is a test 5
this is a test 6
this is a test 7
this is a test 8

Notice that I have somehow lost the first line of the file.
every once in a while I do end up getting the first line.

Can someone Please tell me what's happing to the first line?
What is wrong with my code?

zlatko
18th May 2006, 09:15
Try put \n before first line :rolleyes:

wysota
18th May 2006, 10:45
Try doing it the proper way -- connect the readyReadStandardOutput() signal from your process to a slot and read the data there, and don't read a single line, but rather all of them at once.


QProcess *process = new QProcess(...);
connect(process, SIGNAL(readyReadStandardOutput ()), this, SLOT(fetchOutput()));
connect(process, SIGNAL(finished ( int, QProcess::ExitStatus )), this, SLOT(fetchRest()));
process->start("...");
//...

void thisClass::fetchOutput(){
QProcess *proc = (QProcess*)sender();
while(proc->canReadLine()){
cout << proc->readLine().constData() << endl;
}
}

void thisClass::fetchRest(){
QProcess *proc = (QProcess*)sender();
cout << proc->readAllStandardOutput().constData();
}