5

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?

Oliver
  • 2,864
  • 1
  • 16
  • 27

1 Answers1

8

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");
leemes
  • 44,967
  • 21
  • 135
  • 183
  • That's interesting! I already tried that, but it did not work. QProcess ignores the working directory for the files. I had to specify them with the full path. Thank you! – Oliver Jun 25 '12 at 19:37
  • 1
    @Oliver Note that working directory != application directory. And maybe QProcess uses even another directory to lookup the files... – leemes Jun 25 '12 at 20:50