result of QProcess and Windows cmd is not the same - why?
-
Hey,
I need a hint where to look for my current issue. Basically I want to use an external cmd tool within my program. The purpose of this external tool is to convert measurement files into another format. Therefore it takes three input parameters like inputfile, signalfile and outputfile.
Therefore I did something like:
QFile fMeasurementFile("measurementfile.zip"); QFile fSignalFile("signalfile"); QFile fOutput("output.csv"); QFile fMyTool("mytool.exe"); QStringList arguments; arguments << "inputfile=" + fMeasurementFile.fileName() + " signalfile" + fSignalFile.fileName() + " outputfile=" + fOutput.fileName()); pProcess->start(fMyTool.fileName(), arguments); qDebug() << endl << "====="; qDebug() << pProcess->program() << pProcess->arguments(); qDebug() << "=====" << endl;And I can also read the output:
void MainWindow::readOutput() { QByteArray bArray = pProcess->readAllStandardOutput().simplified().trimmed(); qDebug() << bArray; }My program itself works. Also the external program gets executed and starts working but terminates then. I first thought it might be a typing mistake somewhere but when I copy the result of pProcess->arguments() to a cmd window the execution works absolutely fine. So there is no issue with the filepaths or so.
Then I tried executing the external programm without QProcess:std::array<char, 128> buffer; std::string result; std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(fMyTool.fileName().toUtf8().constData() + arguments.at(0).toUtf8(), "r"), pclose); if (!pipe) { throw std::runtime_error("popen() failed!"); } while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { result += buffer.data(); } qDebug() << QString::fromStdString(result);... and it works absolutely fine. The external program works as expected.
So I wonder what is different when using QProcess.
Using QProcess the external program terminates with "illegal parameter" and I haven't noticed it first but the output of readOutput(); is:
blablabla ... ILLEGAL PARAMETER inputfile=measurementfile.zip signalfile=signalfile outputfile=output.csv\x07... what is the meaning of "\x07" here? That is the only difference I can figure out in all my test runs. And most important: how to remove it? For me it seems that this is the illegal parameter?
I hope you can give me a hint here despite I cannot provide the full code or even the external program.Thanks!
-
Hey,
I need a hint where to look for my current issue. Basically I want to use an external cmd tool within my program. The purpose of this external tool is to convert measurement files into another format. Therefore it takes three input parameters like inputfile, signalfile and outputfile.
Therefore I did something like:
QFile fMeasurementFile("measurementfile.zip"); QFile fSignalFile("signalfile"); QFile fOutput("output.csv"); QFile fMyTool("mytool.exe"); QStringList arguments; arguments << "inputfile=" + fMeasurementFile.fileName() + " signalfile" + fSignalFile.fileName() + " outputfile=" + fOutput.fileName()); pProcess->start(fMyTool.fileName(), arguments); qDebug() << endl << "====="; qDebug() << pProcess->program() << pProcess->arguments(); qDebug() << "=====" << endl;And I can also read the output:
void MainWindow::readOutput() { QByteArray bArray = pProcess->readAllStandardOutput().simplified().trimmed(); qDebug() << bArray; }My program itself works. Also the external program gets executed and starts working but terminates then. I first thought it might be a typing mistake somewhere but when I copy the result of pProcess->arguments() to a cmd window the execution works absolutely fine. So there is no issue with the filepaths or so.
Then I tried executing the external programm without QProcess:std::array<char, 128> buffer; std::string result; std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(fMyTool.fileName().toUtf8().constData() + arguments.at(0).toUtf8(), "r"), pclose); if (!pipe) { throw std::runtime_error("popen() failed!"); } while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { result += buffer.data(); } qDebug() << QString::fromStdString(result);... and it works absolutely fine. The external program works as expected.
So I wonder what is different when using QProcess.
Using QProcess the external program terminates with "illegal parameter" and I haven't noticed it first but the output of readOutput(); is:
blablabla ... ILLEGAL PARAMETER inputfile=measurementfile.zip signalfile=signalfile outputfile=output.csv\x07... what is the meaning of "\x07" here? That is the only difference I can figure out in all my test runs. And most important: how to remove it? For me it seems that this is the illegal parameter?
I hope you can give me a hint here despite I cannot provide the full code or even the external program.Thanks!
@LeoC said in result of QProcess and Windows cmd is not the same - why?:
QStringList arguments;
arguments << "inputfile=" + fMeasurementFile.fileName() + " signalfile" + fSignalFile.fileName() + " outputfile=" + fOutput.fileName());This is wrong, should be:
QStringList arguments; arguments << "inputfile=" + fMeasurementFile.fileName() << "signalfile=" + fSignalFile.fileName() << "outputfile=" + fOutput.fileName());Each argument must be an item in the string list.
-
It was the intention of writing all in one line because doing it like you proposed sets a comma after each argument which is not what my external program expects.
@LeoC How should coma come into the parameter list?! @KroMignon is right, you can also check QProcess documentation to see how parameters are passed to the process.
-
It was the intention of writing all in one line because doing it like you proposed sets a comma after each argument which is not what my external program expects.
@LeoC said in result of QProcess and Windows cmd is not the same - why?:
It was the intention of writing all in one line because doing it like you proposed sets a comma after each argument which is not what my external program expects.
Can you explain why there will be comas in command?
QProcessexpect a list of parameters, this is becauseQProcesshas to managed the "special characters" in arguments like spaces.
Otherwise why useQStringListand notQStringas parameters type? -
Hey,
I need a hint where to look for my current issue. Basically I want to use an external cmd tool within my program. The purpose of this external tool is to convert measurement files into another format. Therefore it takes three input parameters like inputfile, signalfile and outputfile.
Therefore I did something like:
QFile fMeasurementFile("measurementfile.zip"); QFile fSignalFile("signalfile"); QFile fOutput("output.csv"); QFile fMyTool("mytool.exe"); QStringList arguments; arguments << "inputfile=" + fMeasurementFile.fileName() + " signalfile" + fSignalFile.fileName() + " outputfile=" + fOutput.fileName()); pProcess->start(fMyTool.fileName(), arguments); qDebug() << endl << "====="; qDebug() << pProcess->program() << pProcess->arguments(); qDebug() << "=====" << endl;And I can also read the output:
void MainWindow::readOutput() { QByteArray bArray = pProcess->readAllStandardOutput().simplified().trimmed(); qDebug() << bArray; }My program itself works. Also the external program gets executed and starts working but terminates then. I first thought it might be a typing mistake somewhere but when I copy the result of pProcess->arguments() to a cmd window the execution works absolutely fine. So there is no issue with the filepaths or so.
Then I tried executing the external programm without QProcess:std::array<char, 128> buffer; std::string result; std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(fMyTool.fileName().toUtf8().constData() + arguments.at(0).toUtf8(), "r"), pclose); if (!pipe) { throw std::runtime_error("popen() failed!"); } while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { result += buffer.data(); } qDebug() << QString::fromStdString(result);... and it works absolutely fine. The external program works as expected.
So I wonder what is different when using QProcess.
Using QProcess the external program terminates with "illegal parameter" and I haven't noticed it first but the output of readOutput(); is:
blablabla ... ILLEGAL PARAMETER inputfile=measurementfile.zip signalfile=signalfile outputfile=output.csv\x07... what is the meaning of "\x07" here? That is the only difference I can figure out in all my test runs. And most important: how to remove it? For me it seems that this is the illegal parameter?
I hope you can give me a hint here despite I cannot provide the full code or even the external program.Thanks!
@LeoC said in result of QProcess and Windows cmd is not the same - why?:
... what is the meaning of "\x07" here?
Just for this bit. (For the rest follow what the others are telling you.) Rather weirdly, ASCII character 7 is the "bell" sound :) I'm pretty sure that if you ran whatever bad command from a terminal it would output what you see as the error message and you would hear a "beep", which it's outputting to alarm you! :)
-
ooohm, maybe I misunderstood the output of pProcess->arguments() because writing something like this:
arguments << "outputfile=" + fMeasurementFile.fileName(); arguments << "signalfile=" + fSignalFile.fileName(); arguments << "outputfile=" + fOutput.fileName();gives me
"outputfile=measurement.zip", "signalfile=signalfile", "outputfile=output1338.csv"as an output. My assumption was that I can copy exactly this result to the windows cmd and it should work. But it doesn't.
pProcess->arguments() seems to be rather a list of all the arguments.However, I adapted my code but I still get this "\x07" at the end of my last paremter...
-
ooohm, maybe I misunderstood the output of pProcess->arguments() because writing something like this:
arguments << "outputfile=" + fMeasurementFile.fileName(); arguments << "signalfile=" + fSignalFile.fileName(); arguments << "outputfile=" + fOutput.fileName();gives me
"outputfile=measurement.zip", "signalfile=signalfile", "outputfile=output1338.csv"as an output. My assumption was that I can copy exactly this result to the windows cmd and it should work. But it doesn't.
pProcess->arguments() seems to be rather a list of all the arguments.However, I adapted my code but I still get this "\x07" at the end of my last paremter...
@LeoC said in result of QProcess and Windows cmd is not the same - why?:
gives me
"outputfile=measurement.zip", "signalfile=signalfile", "outputfile=output1338.csv"as an output. My assumption was that I can copy exactly this result to the windows cmd and it should work. But it doesn't.
pProcess->arguments() seems to be rather a list of all the arguments.Please do not mixup what different classes does with a
QStringList.
I suppose thatoutputfile=measurement.zip", "signalfile=signalfile", "outputfile=output1338.csv"was the output generated withqDebug() << arguments;. This does only show the content of the QStringList, but there is no link withQProcess.And what do you mean with "\x07" at the end of the last parameter?
It is the output of the QProcess called application?As @JonB already wrote before '\x07' is the ASCII code for BEL, which is often use to make a sound in console application.
I guess this is added by the application to alert user about error.Are you sure you are using the rigth parameter format?
Most of the program I use needs a dash (or double dash) before parameters, maybe it is the case for your application? -
ooohm, maybe I misunderstood the output of pProcess->arguments() because writing something like this:
arguments << "outputfile=" + fMeasurementFile.fileName(); arguments << "signalfile=" + fSignalFile.fileName(); arguments << "outputfile=" + fOutput.fileName();gives me
"outputfile=measurement.zip", "signalfile=signalfile", "outputfile=output1338.csv"as an output. My assumption was that I can copy exactly this result to the windows cmd and it should work. But it doesn't.
pProcess->arguments() seems to be rather a list of all the arguments.However, I adapted my code but I still get this "\x07" at the end of my last paremter...
@LeoC said in result of QProcess and Windows cmd is not the same - why?:
However, I adapted my code but I still get this "\x07" at the end of my last paremter...
That character is not a parameter to your command, is it? It's a character tagged onto the end of the output "ILLEGAL PARAMETER ...." mesage, isn't it? Please don't call that a "parameter" (they are used as arguments/options/parameters TO the program), that just confuses. Since you don't care about it, simply remove or ignore that character when looking at what you get back.
What exactly do you type into a Command Prompt to run your external command to make it work from there? Please copy & paste an exact, working command line.
-
@LeoC said in result of QProcess and Windows cmd is not the same - why?:
QStringList arguments;
arguments << "inputfile=" + fMeasurementFile.fileName() + " signalfile" + fSignalFile.fileName() + " outputfile=" + fOutput.fileName());Create a stringlist separating out your arguments as others have said, but your original code was also missing an equal sign after 'signalfile'.
-
First of all I would like to thank you for all the inputs.
Let's try to sort everything out.This is the command which works fine when using the windows cmd
C:\myTool.exe inputfile=C:/mytmp/measurement.zip;signalfile=C:/mytmp/signals outputfile=C:/mytemp/output.csvThe cmd then gives me the following output:
UNZIP C:\mytmp\measurement.zip... OK c:\users\...\appdata\...\measurement.unzipped READING c:\users\...\appdata\...\measurement.unzipped ... OK PROCESSING FILE c:\mytemp\signallist ... OK WRITING C:\mytemp\output.csv ... OK FINISHED ...After adjusting my argument list the result of pProcess->arguments is:
"C:\myTool.exe" ("inputfile=C:/mytmp/measurement.zip", ";signalfile=C:/mytmp/output.csv", "outputfile=C:/mytmp/output.csv")Since I connected the signal readyReadStandardOutput() I can see the result which is:
ILLEGAL PARAMETER ;signalfile=C:/mytmp/output.csv\x07"And now the question is: where is the difference between using the windows cmd and QProcess since cmd works fine and QProcess does not.
edit: After reading my own post again I guess it should rather look like this... ?
"C:\myTool.exe" ("inputfile=C:/mytmp/measurement.zip;signalfile=C:/mytmp/output.csv", "outputfile=C:/mytmp/output.csv") -
First of all I would like to thank you for all the inputs.
Let's try to sort everything out.This is the command which works fine when using the windows cmd
C:\myTool.exe inputfile=C:/mytmp/measurement.zip;signalfile=C:/mytmp/signals outputfile=C:/mytemp/output.csvThe cmd then gives me the following output:
UNZIP C:\mytmp\measurement.zip... OK c:\users\...\appdata\...\measurement.unzipped READING c:\users\...\appdata\...\measurement.unzipped ... OK PROCESSING FILE c:\mytemp\signallist ... OK WRITING C:\mytemp\output.csv ... OK FINISHED ...After adjusting my argument list the result of pProcess->arguments is:
"C:\myTool.exe" ("inputfile=C:/mytmp/measurement.zip", ";signalfile=C:/mytmp/output.csv", "outputfile=C:/mytmp/output.csv")Since I connected the signal readyReadStandardOutput() I can see the result which is:
ILLEGAL PARAMETER ;signalfile=C:/mytmp/output.csv\x07"And now the question is: where is the difference between using the windows cmd and QProcess since cmd works fine and QProcess does not.
edit: After reading my own post again I guess it should rather look like this... ?
"C:\myTool.exe" ("inputfile=C:/mytmp/measurement.zip;signalfile=C:/mytmp/output.csv", "outputfile=C:/mytmp/output.csv")@LeoC said in result of QProcess and Windows cmd is not the same - why?:
This is the command which works fine when using the windows cmd
C:\myTool.exe inputfile=C:/mytmp/measurement.zip;signalfile=C:/mytmp/signals outputfile=C:/mytemp/output.csvIf this is what you want to have, you should do it like this:
QStringList arguments; arguments << "inputfile=" + fMeasurementFile.fileName() + ";signalfile=" + fSignalFile.fileName() << "outputfile=" + fOutput.fileName()); -
Sorry for not comming back earlier. I was forced to do some other things in between...
But it seems that doing something different for 3 weeks was the right thing to do.I opened the project again, checked every line , adjusted it a bit (including the proposal of @KroMignon ) and now... it works :)
Thanks to everyone involved here.