Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QProcess start with files from stdin and stdout

I need to run following statement from QProcess:

programm < file1 > file2

in QT:

QProcess *proc = new QProcess;
proc->setReadChannelMode(QProcess::SeparateChannels);
proc->start("program < \"file1\" > \"file2\"", QIODevice::ReadWrite);

But somehow it does not work. I see in taskmanager, that the command looks correctly, but it seems as the program is executed without any arguments. Where is my error?

like image 916
Oliver Avatar asked Feb 21 '23 01:02

Oliver


1 Answers

Reading from and writing into files using < respectively > is a syntax feature of the shell. If you run the command line programm < file1 > file2 using a shell like sh, the command program gets executed only, with no arguments at all. Assigning the programs channels for input and output to the given files doesn't have anything to do with the command itself.

But QProcess can be told to simulate this behaviour by using these methods:

QProcess::setStandardInputFile(QString fileName) QProcess::setStandardOutputFile(QString fileName)

So your code becomes:

QProcess *proc = new QProcess;
proc->setReadChannelMode(QProcess::SeparateChannels);
proc->setStandardInputFile("file1");
proc->setStandardOutputFile("file2");
proc->start("program");
like image 110
leemes Avatar answered Feb 22 '23 13:02

leemes