1

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 2
    The problem is not with how `QProcess::write` sends text to your application - the problem is with how that application reads it. `cin >> action` reads a sequence of characters up to the first whitespace, so it can't possibly read `"several words"` as a single `action`. You are likely looking for [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline) – Igor Tandetnik Mar 12 '17 at 18:31
  • Standard IO streams are streams. You can't write "messages" and expect there to be some invisible message border marks. It's all just individual characters, one after the other. With your code using *cin* that way, any whitespace is interpreted as separator, so you get one word at a time and all whitespace is removed. – hyde Mar 12 '17 at 18:43

0 Answers0