Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
problem with System()
-
Hello everyone:
First of all thanks a lot for reading this post and being able to help.
I have a problem whit System command.if I write:
sprintf(command, "%s %0.0f %0.0f 0 %0.0f %0.0f %s 0 0 +", princi,wps.front().x(), wps.front().y(),wps.back().x(),wps.back().y(),name_bmp); chdir("/home/"); //To change directory system(command);
It give me segmentation fault when it runs system(command)
However if I write:
system("./princi 0 747.37 427.68 0 844.37 42.54 prueba_princi.bmp 0 0 +")
it runs perfectly....
What am I doing wrong?
Thanks!
-
Hi,
What does it have to do with Qt ?
In any case, check the content of your command string.
Note that you can use QProcess for that kind of task.
-
@SGaist Yes, command string is right... but I do not why segmentation fault...
I am going to check QProcess
-
This is not really an answer to your question but still related: You should consider using the QProcess class. This is not only a lot more flexible & portable than the
system()
function but also integrates well into the Qt framework (signal&slots, properties, ...).
Usually you want to use Qt library components wherever possible.To still be somewhat useful: Did you check that
command
is large enough and that the string assembled bysprintf()
is correct?Edit: @SGaist played ninja again....
-
@AlvaroS said:
sprintf(command, "%s %0.0f %0.0f 0 %0.0f %0.0f %s 0 0 +", princi,wps.front().x(), wps.front().y(),wps.back().x(),wps.back().y(),name_bmp)
What types are
princi
name_bmpbetter not be QStrings :)
-
@mrjj Hello.
They are const char* !
-
Then run your application through the debugger.
-
@SGaist Now I do this:
QString program = "./home/princi"; QStringList arguments; arguments << "0" << QString::number(wps.front().x()) << QString::number(wps.front().y()) << "0" << QString::number(wps.back().x()) << QString::number(wps.back().y()) << "prueba_princi.bmp" << "0 0 +" ; QProcess myProcess; myProcess.start(program,arguments);
it compiles good but my application does not run...
-
You call it with a path relative to where you main application is started.
./home/princi
is equivalent to$PWD/home/princi/
which is likely not what you want, is it ?
-
@SGaist yes! but it does not run with that code..
-
@SGaist Now I do this and nothing as well..
QString program = "princi"; QStringList arguments; arguments << "0" << QString::number(wps.front().x()) << QString::number(wps.front().y()) << "0" << QString::number(wps.back().x()) << QString::number(wps.back().y()) << "prueba_princi.bmp" << "0 0 +" ; QString folder = "/home/"; QProcess *myProcess = new QProcess(this); myProcess->setWorkingDirectory(folder); myProcess->start(program,arguments);
-
What is the full path to
princi
? And what is the full path to your application ?
-
@SGaist Full path of princi (which is the executable) is /home/
and full path of my QT aplication is /home/workspace
-
/home is not the full path to your application except it is called home and is located in the root directory.
So, you can start the program in a shell with /home/princi?
Did you try it?
This is quite strange path: in /home you usually have a directory for each user and /home is not writable by normal user.
-
@jsulm sorry yes, it is in /home/user1/ but it does not run as well...
-
-
@mrjj It works in the same way... :(
-
@AlvaroS So, if you enter /home/user1/princi in a terminal window and press enter it does not start? What happens? Any error message?
-
Ok, you must debug then
try to use
http://doc.qt.io/qt-5/qprocess.html#error
to see what it thinks.
-
@jsulm In a terminal if I write
./princi 0 747.37 427.68 0 844.37 42.54 prueba_princi.bmp 0 0 +
in /home/user1/ directory it runs good
-
-
This post is deleted!
-
@AlvaroS said:
@jsulm In a terminal if I write
./princi 0 747.37 427.68 0 844.37 42.54 prueba_princi.bmp 0 0 +
in /home/user1/ directory it runs good
@jsulm said:
okey, it gives me UnknowError.... http://doc.qt.io/qt-5/qprocess.html#ProcessError-enum
-
well you can use
qDebug() "qproc: " << myProcess->error();and check the code in
http://doc.qt.io/qt-5/qprocess.html#ProcessError-enum
-
@mrjj Yes thanks!! See my last post. Thanks again. It gives me Unknow error...
-
" This is the default return value of error()."
You do call it AFTER u call
myProcess->start(program,arguments); ?
-
@mrjj Yes:
myProcess->start(program, arguments); QProcess::ProcessError error = myProcess->error();
-
@AlvaroS
hmm really odd then
it should say FailedToStartJust to be 100% clear.
princi is never run ?
-
@mrjj said:
@AlvaroS
hmm really odd then
it should say FailedToStartJust to be 100% clear.
princi is never run ?
okey look.
if I write:myProcess->start(program, arguments); myProcess->waitForFinished(3000); QProcess::ProcessError error = myProcess->error();
Now error says:
QProcess::Crashed The process crashed some time after starting successfully.
-
so it sounds like your princi do run but it crashes?
oh. sorry my bad. start is async so calling error right after was
not correct.waitForFinished fixed that.
-
@mrjj Yes... I think so...
How should I pass the arguments?the arguments of princi are:
0 wps.front().x() wps.front().y() 0 wps.back().x() wps.front().y() prueba_princi.bmp 0 0 +
so my QStringList arguments is:
QStringList arguments; arguments << "0" << QString::number(wps.front().x()) << QString::number(wps.front().y()) << "0" << QString::number(wps.back().x()) << QString::number(wps.back().y()) << "prueba_princi.bmp" << "0" << "0" << "+" ;
-
it seems fine
You should look inside princi for how it
reads it. it must be that part that is crashing ?
-
@mrjj yes, but it really rare that if I run from terminal princi runs good...
-
@mrjj And if I write:
system ("./princi 0 5 10 0 7 8 prueba_princi.bmp 0 0 +")
where 5,10,7 and 8 are wps.front,wps.back....
it runs well...
-
What is that + sign doing as parameter ?
-
@SGaist + is just and argument that i have to pass to princi
-
@AlvaroS
well you must debug it then.
Try to qDebug inside princi what you read and compare that to what u read for
system ("./princi 0 5 10 0 7 8 prueba_princi.bmp 0 0 +")Sadly We cannot guess why you crash from the code shown.
Also you dump the arguments to qDebug before u
call start, just to see what u are sending also.and then take it from there.