running shell command from C++ code problem
Hi,
My program needs to get a list of users on the system. From linux shell it works fine:
# who | cut -d' ' -f1 | sort -u
When I start this from code I get an error with exit code 255. I've read that this means that some libraries can't be found. What libraries are necessary and how and where to declare them for C++ program? Are these libraries for who, cut and sort commands?
Code:
exec.start( "who|cut -d' ' -f1|sort -u" );
exec.waitForFinished();
int exitcode = exec.exitCode(); // exitcode is always 255!
Thanks!!
Re: running shell command from C++ code problem
You need a shell to interpret the command. See for example this post.
Re: running shell command from C++ code problem
This works fine, thanks a million!!
Code:
arguments << "-c" << "who|cut -d' ' -f1|sort -u";
exec.start("/bin/sh", arguments);
Re: running shell command from C++ code problem
How could we adjust this code to windows command?
should we replace /bin/sh with win32 or something?
thanks
SunnySan
Re: running shell command from C++ code problem
Hi,
use cmd.exe with /c
Code:
arguments << "/c dir /b e:\\test\\*.txt | sort";
exec.start("cmd.exe", arguments);
exec.waitForFinished();
qDebug() << exec.readAllStandardOutput();
HTH, Bernd