Results 1 to 4 of 4

Thread: QProcess to interact with external application that uses sscanf/printf

  1. #1
    Join Date
    Jul 2007
    Posts
    56
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question QProcess to interact with external application that uses sscanf/printf

    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:

    Qt Code:
    1. printf("enter a number\n");
    2. scanf("%d",&number);
    3. printf("you entered %d",number);
    To copy to clipboard, switch view to plain text mode 

    I don't get anything until I call
    Qt Code:
    1. myProcess->write("4\r");
    To copy to clipboard, switch view to plain text mode 
    ,then I suddenly get:
    Qt Code:
    1. enter a number
    2. you entered 4
    To copy to clipboard, switch view to plain text mode 

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

    I'm using QProcess as follows:

    Qt Code:
    1. myProcess = new QProcess(this);
    2. connect(myProcess,SIGNAL(readyReadStandardOutput()), this, SLOT(data_ready()));
    3. myProcess->start("testapp");
    4. if (!myProcess->waitForStarted(5000))
    5. {
    6. return -1;
    7. }
    To copy to clipboard, switch view to plain text mode 


    Am I misunderstanding how QProcess should work?

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QProcess to interact with external application that uses sscanf/printf

    you must use unbuffered io or call flush to make sure the 'written' data gets really written

  3. #3
    Join Date
    Jul 2007
    Posts
    56
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess to interact with external application that uses sscanf/printf

    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?

  4. #4
    Join Date
    Nov 2014
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QProcess to interact with external application that uses sscanf/printf

    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.

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 11:12
  2. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 23:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.