Need help!!!!! Problem in running a program with the help of QProcess



  • First of all I make a file which reads program written on plainTextEdit and batch file for output and i am able to reterive the output from batch file and display it to another plainTextEdit but problem arises in case of cin and scanf statements .For example :

    @
    #include<iostream>
    using namespace std;
    int main()
    {
    int a,b;
    cout<<"Enter two numbers";
    cin>>a>>b;
    return 0;
    }
    @

    When i compiled this program a batch file will open that does not contain Enter two numbers statement that means it can take the input only but does show cout/printf statements,that is creating problem because user will not be able to detect what to enter. As i understands the problem and know what to enter in a batch file suppose i enetered 2 3 so output reterive to a plainTextEdit is correct i.e Enter two numbers 2 3. But problem occurs at User Console/Command Prompt.


  • Moderators

    Please introduce code wrappings into your post. For your little program it is not really required, but anyway the code wrappings code sections easier to read.

    This is a buffer problem. Try a "flush":http://www.cplusplus.com/reference/ostream/flush-free/ to make sure that the text is written.



  • @
    bool MainWindow::run_file()
    {
    QMessageBox msgBox;
    QString program = "cons.bat";
    QString st="PRESS OKK!!";
    QStringList arguments;
    QProcess *proc = new QProcess(this);
    proc->startDetached(program);

    if(proc->NotRunning)
    

    msgBox.setText(st);
    msgBox.exec();
    proc->waitForFinished();

    display_output();
    return(true);
    

    }
    void MainWindow::display_output()
    {

    QFile file&#40;"out.txt"&#41;;
    if(!file.open(QIODevice::ReadOnly&#41;) {
            QMessageBox::information(0, "error", file.errorString());
       }
    //QTextStream str(&file);
    
    QString out_str;
    
    while (!file.atEnd())
    {
        out_str=file.readAll();
    }
    ui->plainTextEdit_3->clear();
    ui->plainTextEdit_3->insertPlainText(out_str);
    file.remove();
    

    }
    @


  • Moderators

    In previous I have forgotten to give the "link to code wrappings":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 for some reason.
    I will introduce them this time.

    [quote author="koahnig" date="1391594342"]
    This is a buffer problem. Try a "flush":http://www.cplusplus.com/reference/ostream/flush-free/ to make sure that the text is written.[/quote]
    I think this is still valid. Try a flush on cout there.



  • I think flush is used to delete cout statements but i don't want this. I want cout statement should print on console window. Console window is able to take input but unable to show cout statements.


  • Moderators

    No, flush writes actually out. "Read the comments which are given through this link.":http://www.cplusplus.com/reference/ostream/flush-free/

    Typically also cout is buffered until enough to write. So flush makes sure that information is pushed out.



  • Can you help me how to use flush in my code?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.