PDA

View Full Version : QProcess to interact with external application that uses sscanf/printf



will49
23rd July 2008, 07:31
I'm trying to use QProcess to interact with a very simple external application on Linux that takes some input using scanf and outputs results using printf.

My understanding is that I can use the the signal "ReadyReadStandardOutput" to tell me when my external app has some text output ready for me to read, and I can then send it some text using write() and that will go into the scanf() of the application.

However it doesn't actually output any text to my "ReadyReadStandardOutput" slot until the application has quit. Then I get a buffer with all of the text output from the prinf().

For example in my external app:


printf("enter a number\n");
scanf("%d",&number);
printf("you entered %d",number);

I don't get anything until I call
myProcess->write("4\r");,then I suddenly get:

enter a number
you entered 4

I was expecting that I should get "enter a number" as soon as I start the QProcess.

I'm using QProcess as follows:


myProcess = new QProcess(this);
connect(myProcess,SIGNAL(readyReadStandardOutput() ), this, SLOT(data_ready()));
myProcess->start("testapp");
if (!myProcess->waitForStarted(5000))
{
return -1;
}


Am I misunderstanding how QProcess should work?

caduel
23rd July 2008, 07:45
you must use unbuffered io or call flush to make sure the 'written' data gets really written

will49
23rd July 2008, 13:29
Do you mean the app that I'm calling with QProcess needs to use unbuffered IO? How can I make prinf() unbuffered? Should I change it to use a stream then call flush on it?

If you are meaning I need to set my QProcess to use unbuffered IO, then I don't see a way to do that or how to flush. Also I should at least receive the "enter a number" because that is output as soon as the app runs - right?

diguang1314@sina.com
20th November 2014, 07:10
you must use unbuffered io or call flush to make sure the 'written' data gets really written !

Example, use unbuffered:

setvbuf(stdout, (char *)NULL, _IONBF, 0);

add above code to your external app. I meet the same questions,and resolved it at last.