简体   繁体   中英

Qt | QProcess | Write line to console application

I am using QProcess to communicate with console application: i am writing some words and read outputs. But i want to write line via QProcess. For example, i have the next console app:

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
    string action;
    do
    {
        cout << "(test)";
        cin >> action;
        cout << action;
        if(action.length() > 10)
        {
            cout << "\t very long string";
        }
        cout << endl;
    }
    while(action != "exit");
    return 0;
}

So, i can't pass parameters via QProcess::exec or QProcess::start, because it pass parameters to char* argv[]. I should pass it after launching QProcess. I've tried use QProcess::write, but there are problem: if i use

process.write("oneWord\n");

i'll get success. But if i use

process.write("several words\n");

my program will write all this word separately and it looks like

process.write("several\n");
process.write("words\n");

Console application doesn't recognize it as one string. I've tried using different ways: write line in double brackets,

process.write("\"several words"\\n");

and

process.write("\"several words\n""\);

use protected methods QIODEvice::setOpenMode and set QIODevice::Text flag, use QDataStream , use special symbols like \\r , \\n , \\t and different combination. Also, i tried to use multiple QProcess::write

process.write("several");
process.write("words\n");

I know, that QProcess inherits QIODevice and there are opportunity to deal with it like a file (input & output). But it doesn't matter if words will be written to file separately in file. In my case, it does matter.

Can anyone help me?

This answer addresses your issue, I believe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM