0

I want execute a commande line with QProcess :

  QString elf_path=C:\\files\\file.elf;
  QString appli = "readelf.exe -a "+elf_path+" >>C:\\work\\essai.txt";
  QProcess *process = new QProcess();
  process->execute(appli);

but QT display this error :

 readelf: Error: '>>C:\work\essai.txt': No such file

Can you help me ?

physics
  • 161
  • 9

3 Answers3

1

The QProcess::execute command will take the first parameter as the executable and pass each of the next parameters as arguments to that executable. So the error is because the readelf executable is receiving ">>C:\work\essai.txt" as an argument.

There is more than one solution to fix this.

Rather than redirecting the output to the text file, you could read the output from the readelf command (readAllStandardOutput), open a file essai.txt from Qt and append the output yourself. You should probably call waitForFinished() before retrieving the output.

Alternatively, there's a function in QProcess called setStandardOutputFile which takes a filename to redirect the output from the process to that file, which may be easier: -

QProcess* proc = new QProcess;
QString appli = "readelf.exe -a " + elf_path;
proc->setStandardOutputFile("C:\\work\\essai.txt", QIODevice::Append);
proc->start(appli);

Finally, you could create a shell script and call that with your parameters where the shell script would know that the final input parameter is to use for the output redirection.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
0

QProcess::execute is static method. You should not create instance of QProcess in your case. Try next code

const QString path2exe = "readelf.exe";
QStringList commandline;
commandline << "-a";
commandline << elfPath;
commandline << "c:\\work\\essai.txt"
QProcess::execute( path2exe, commandline );
Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
0

It looks like readelf is seeing your redirection as another file, which is valid since readelf can handle more than one on the command line.

Hence, the Qt process stuff is not handling redirection as you expect.

Within a shell of some sort, the redirections are used to set up standard input/output (and possibly others) then they're removed from the command line seen by the executable program. In other words, the executable normally doesn't see the redirection, it just outputs to standard output which the shell has connected to a file of some sort.

In order to fix this, you'll either have to run a cmd process which does understand redirection (passing the readelf command as a parameter) or use something like the method QProcess::readAllStandardOutput() to get the output into a byte array instead of writing to a temporary file.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953