Interact with QProcess - reading and writing to QProcess results in no output - c++

noted: this is a use-specific question, but I hope others can benefit from this too
To get a basic sense of what I want to do:
QProcess runs a command by:
QProcess::start("sh -c \"cd /tmp/tempdir ; ./my_script --option file.txt ; echo $?\" ")
The script expects input from the user (a password), QProcess::readAll() confirms the script's input request. The input is given by QProcess::write().
Here I get lost! -> No output is received. from readAll() and readAllStandardOutput(), both are empty.
I need the output: particularly the echo $? for later processing, but don't get anything since readAll() and readAllStandardOutput() returns empty strings
To me there are several possible issues to cause this, maybe more than one:
The script does not receive this input
The script receives the input, but expects a "return key press"
The script receives the input, QProcess::write automatically does a "return key press", the script continues and finishes but QProcess does not read this output
The Code:
// please ignore the messiness of the code, a placed a number of debugs just to see the code response.
cmd = "cd /tmp/tempdir ; ./my_script --option file.txt ; echo $?" (without quotes)
input => required input for script
QString gen_serv::runCommand(QString cmd, QString input){
Process *p = new QProcess(parent);
p->setProcessChannelMode(QProcess::MergedChannels);
// p->start("sh ", QStringList() << " -c " << cmd);
QString c = QString("sh -c \"" + cmd + "\" ");
p->start(c); <---- ACTUAL COMMAND : sh -c "cd /tmp/tempdir ; ./my_script --option file.txt ; echo $?"
if (p->waitForStarted()) {
if (!p->waitForReadyRead()) {
qDebug(log_lib_gen_serv) << "waitForReadyRead() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
}
if (!p->waitForFinished(1000)) {
qDebug() << p->readAll();
qDebug() << p->readAllStandardOutput();
//p->write(input.toLatin1());
p->write(QString(input + QString("\n")).toLatin1()); <--- added this "\n" incase the process/console was waiting for a return press, thus the /n should satisfy the return requirement
qDebug() << p->readAll();
qDebug() << p->readAllStandardOutput();
if (!p->waitForFinished()) {
qDebug() << p->readAll();
qDebug() << p->readAllStandardOutput();
qDebug(log_lib_gen_serv) << "waitForFinished() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
}
qDebug() << p->readAll();
qDebug() << p->readAllStandardOutput();
}
QString s = QString(p->readAll() + p->readAllStandardOutput());
return s;
}
else{
qDebug(log_lib_gen_serv) << "waitForStarted() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
}
p->waitForFinished();
p->kill();
return QString();
}
Please note:
The script is long and complicated, however it returns a message "done" if successfully executed and an exit code for each case.
Thus the echo $? should capture and return this for processing i.e. return s
I hope this makes sense, any suggestions how I can attempt to solve this issue?

Related

'\r\n' deletes the whole buffer when the length is too long

I'm learning socket programming and there is a requirement in my project to put \r\n in every returned message. Something I notice that \r\n will delete the whole buffer when it exceeds some number of characters. For example, I have a code like this:
#include <iostream>
int main()
{
std::string message = "mkdir homewor";
const char *buffer = (message + "\r\n").c_str();
std::cout << "message: " << message << std::endl;
std::cout << "buffer: " << buffer << std::endl;
}
I run and it gives me the output like this:
message: mkdir homewor
buffer: mkdir homewor
I open the gdb and it gives me something like this:
message: "mkdir homewor"
buffer: 0x7fffffffdc50 "mkdir homewor\r\n"
- *buffer: 109 'm'
Which is something I expect it.
But when the message is too long and I convert it to C-String, the whole buffer gets deleted. For example:
#include <iostream>
int main()
{
std::string message = "mkdir homework"; // Just one more 'k' at the end
const char *buffer = (message + "\r\n").c_str();
std::cout << "message: " << message << std::endl;
std::cout << "buffer: " << buffer << std::endl;
}
I run and it gives me the output:
message: mkdir homework
buffer:
gdb gives me this:
message: "mkdir homework"
buffer: 0x55555556aeb0 ""
- *buffer: 0 '\000'
One more observation is that if the message has or exceeds the length of the message in the second example, the buffer will be deleted no matter what. Can anyone tell me what this problem is? I cannot get rid of \r\n because it's required in the project.
Thanks in advance.
(message + "\r\n") creates a new std::string, and it is backed with a pointer.
This new std::string will get destroyed at the end of the full-expression.
So you should do this,
#include <iostream>
int main()
{
std::string message = "mkdir homework"; // Just one more 'k' at the end
std::string messageSuffixed = message + "\r\n";
const char *buffer = messageSuffixed.c_str();
std::cout << "message: " << message << std::endl;
std::cout << "buffer: " << buffer << std::endl;
}

How to run command line with QProcess?

i have a application (X)Medcon, i want to run command line (convert file) with QProcess. I tried but it is not success. This is my code
convertDicomProcess = new QProcess(this);
QString program = "C:\\Program Files\\XMedCon\\bin\\xmedcon.exe";
QStringList arguments;
arguments << "medcon"<< "-f" << "F:/33.nii" << "-c" << "dicom" << "-o" << "F:/33.dcm";
convertDicomProcess->start(program, arguments);
convertDicomProcess->waitForFinished();
QByteArray output = convertDicomProcess->readAll();
convertDicomProcess->close();
When i run command line with
medcon -f E:\55.nii -c dicom -o
E:\55.dcm
it is convert success
Try:
QStringList arguments;
arguments << "/c" << program << "-f" << "F:/33.nii" << "-c" << "dicom" << "-o" << "F:/33.dcm";
convertDicomProcess->start("cmd.exe", arguments);
Test if you really need "medcon" as an argument again, I do not know since I do not know the "medcon" program. If yes change it to:
arguments << "/c" << program << "medcon" << "-f" << "F:/33.nii" << "-c" << "dicom" << "-o" << "F:/33.dcm";
This code tries to run the medcon program in a shell.
If you path exists, I think you need to use quotes (\") in the string for the one:
QString program = "\"C:\\Program Files\\XMedCon\\bin\\xmedcon.exe\"";
...

reading and writing to QProcess in Qt Console Application

Noted: this appears to be a specific issue question but hopefully it can be edited for all to related to
I need to interact with a QProcess object.
The Problem:
I am not getting any output from QProcess after calling QProcess:write(input)
More Info:
Going through the doc pages led me to create an example below:
I have a script requesting user input, and finally displaying and appropriate message based on the user input.
Testing:
After adding a "log" feature to my script for testing, the following occurs:
script executes
script requests user input (confirmed by the 'first' qDebug() << p->readAll())
script accepts input from QProcess (confirmed by script 'log output')
After this, no output is received. The following 2 debug statements both fire (i.e. wait 30s each)
if (!p->waitForReadyRead()) {
qDebug() << "waitForReadyRead() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
}
if (!p->waitForFinished()) {
qDebug() << "waitForFinished() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
}
Followed by:
QString s = QString(p->readAll() + p->readAllStandardOutput());
where s is an empty string.
The issue is s should contain either "success" or "failed"
Calling Code:
QString cmd = QString("sh -c \"/path/to/bashscript.sh\"");
QString input = QString("Name");
QString result = runCommand(cmd, input)
Process Code:
//takes 2 parameters,
// cmd which is the code to be executed by the shell
// input which acts as the user input
QString runCommand(QString cmd, QString input){
QProcess *p = new QProcess(new QObject());
p->setProcessChannelMode(QProcess::MergedChannels); //no actual reason to do this
p->start(cmd);
if (p->waitForStarted()) {
if (!p->waitForReadyRead()) {
qDebug() << "waitForReadyRead() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
}
if (!p->waitForFinished()) {
//reads current stdout, this will show the input request from the bash script
//e.g. please enter your name:
qDebug() << p->readAll();
//here I write the input (the name) to the process, which is received by the script
p->write(ps.toLatin1());
//the script should then display a message i.e. ("success" o "failed")
if (!p->waitForReadyRead()) {
qDebug() << "waitForReadyRead() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
}
if (!p->waitForFinished()) {
qDebug() << "waitForFinished() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
}
}
QString s = QString(p->readAll() + p->readAllStandardOutput());
return s;
}
else{
qDebug() << "waitForStarted() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
}
p->waitForFinished();
p->kill();
return QString();
}
script.sh (-rwxrwxr-x)
#!/bin/bash
#returns
# "success" on non empty $n value
# "failed: on empty $n value
#
echo "enter your name:"
read n
if [[ ! -z $n ]];
then
echo "success"
exit 0;
else
echo "failed"
exit 1;
fi
UPDATE
#KevinKrammer I modified the run command as you said, also using the QStringList with the args.
Still does not get output, infact the waitForReadyRead() and waitForFinished() returns false instantly.
Called with:
QString r = runCommand(QString("text"));
Process Code:
QString runCommand(QString input){
QProcess *p = new QProcess(new QObject());
p->setProcessChannelMode(QProcess::MergedChannels);
//script is the same script refered to earlier, and the `cd /home/dev` IS required
p->start("sh", QStringList() << "-c" << "cd /home/dev" << "./script");
;
if (p->waitForStarted()) {
if (!p->waitForReadyRead(5000)) {
qDebug() << "waitForReadyRead() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
}
qDebug() << p->readAll();
p->write(input.toLatin1());
if(!p->waitForFinished(5000)){
qDebug() << "waitForFinished() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
}
QString s = QString(p->readAll() + p->readAllStandardOutput());
return s;
}
else{
qDebug() << "waitForStarted() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
}
p->waitForFinished();
p->kill();
return QString();
}
Terminal Output of the Process:
started
readChannelFinished
exit code = "0"
waitForReadyRead() [false] : CODE: "5" | ERROR STRING: "Unknown error"
""
waitForFinished() [false] : CODE: "5" | ERROR STRING: "Unknown error"
Press <RETURN> to close this window...
Thoughts on this?
UPDATE 2
#Tarod Thank you for taking the time to make a solution.
It works, however not completely is expected.
I copied over your code, exactly.
Made a few changes in the mReadyReadStandardOutput()
See additional info below.
The problem:
After running the application (and script), I get a result -> AWESOME
Everytime it is the incorrect result i.e. "failed". -> NOT AWESOME
Terminal Output:
void MyProcess::myReadyRead()
void MyProcess::myReadyReadStandardOutput()
"enter your name:\n"
""
void MyProcess::myReadyRead()
void MyProcess::myReadyReadStandardOutput()
"failed\n"
Press <RETURN> to close this window...
script contents:
#!/bin/bash
echo "enter your name:"
read n
echo $n > "/tmp/log_test.txt"
if [[ ! -z "$n" ]];
then
echo "success"
exit 0;
else
echo "failed"
exit 1;
fi
/tmp/log_test.txt output
myname
running this manually from console:
dev#dev-W55xEU:~$ ls -la script
-rwxrwxr-x 1 dev dev 155 Jan 25 14:53 script*
dev#dev-W55xEU:~$ ./script
enter your name:
TEST_NAME
success
dev#dev-W55xEU:~$ cat /tmp/log_test.txt
TEST_NAME
Full code:
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
class MyProcess : public QProcess
{
Q_OBJECT
public:
MyProcess(QObject *parent = 0);
~MyProcess() {}
public slots:
void myReadyRead();
void myReadyReadStandardOutput();
};
MyProcess::MyProcess(QObject *parent)
{
connect(this,SIGNAL(readyRead()),
this,SLOT(myReadyRead()));
connect(this,SIGNAL(readyReadStandardOutput()),
this,SLOT(myReadyReadStandardOutput()));
}
void MyProcess::myReadyRead() {
qDebug() << Q_FUNC_INFO;
}
void MyProcess::myReadyReadStandardOutput() {
qDebug() << Q_FUNC_INFO;
// Note we need to add \n (it's like pressing enter key)
QString s = this->readAllStandardOutput();
qDebug() << s;
if (s.contains("enter your name")) {
this->write(QString("myname" + QString("\n")).toLatin1());
qDebug() << this->readAllStandardOutput();
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyProcess *myProcess = new MyProcess();
QString program = "/home/dev/script";
myProcess->start("/bin/sh", QStringList() << program);
a.exec();
}
#include "main.moc"
script issue? QProcess issue?
Unfortunately I don't have all your code, so I made an example. I hope it helps you.
If I compare my code to yours, I think the problem could be you are not calling readAllStandardOutput() after writing or maybe you are not calling exec() in your main.cpp.
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
class MyProcess : public QProcess
{
Q_OBJECT
public:
MyProcess(QObject *parent = 0);
~MyProcess() {}
public slots:
void myReadyRead();
void myReadyReadStandardOutput();
};
MyProcess::MyProcess(QObject *parent)
{
connect(this,SIGNAL(readyRead()),
this,SLOT(myReadyRead()));
connect(this,SIGNAL(readyReadStandardOutput()),
this,SLOT(myReadyReadStandardOutput()));
}
void MyProcess::myReadyRead() {
qDebug() << Q_FUNC_INFO;
}
void MyProcess::myReadyReadStandardOutput() {
qDebug() << Q_FUNC_INFO;
// Note we need to add \n (it's like pressing enter key)
this->write(QString("myname" + QString("\n")).toLatin1());
// Next line no required
// qDebug() << this->readAll();
qDebug() << this->readAllStandardOutput();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyProcess *myProcess = new MyProcess();
QString program = "/home/fran/code/myscript.sh";
myProcess->start("/bin/sh", QStringList() << program);
a.exec();
}
#include "main.moc"
Script to test the application:
echo "enter your name:"
read n
if [ ! -z "$n" ];
then
echo "success"
exit 0;
else
echo "failed"
exit 1;
fi

cpp popen get ls error [duplicate]

This question already has answers here:
c popen won't catch stderr
(2 answers)
Closed 6 years ago.
I have a directory that when i run a ls command on, I know that it spits out an error like so:
ls: reading directory /mydir: Input/output error
I want to be able to detect that there was an IO error in my code.
This is what i've tried:
void readLs(const std::string& name)
{
stringstream ss;
ss << "ls " << name;
FILE* apipe = popen(ss.str().c_str(), "r");
if(apipe == NULL)
{
cout << "Error opening popen" << endl;
return;
}
char line[256];
cout << __FILE__ << " " << __LINE__ << endl; //This is line 46
while ( fgets( line, 256 , apipe) )
{
string temp(line);
cout << "This is line: " << temp << endl;
memset(line, 0, 256);
}
cout << __FILE__ << " " << __LINE__ << endl; //This is line 53
pclose(apipe);
}
test.cpp 46
ls: reading directory /mydir: Input/output error
test.cpp 53
The error message prints out to the screen but i don't get the error message when reading from the pipe.
Thanks,
Conversely, reading from the stream reads the command's
standard output
http://man7.org/linux/man-pages/man3/popen.3.html
You are reading the standard input of the invoked program, which is empty. The error message is written to the standard output. Try executing ls 2>&1.

Trying to execute .exe from std::cout

I have a C++ program that reads a config file and gets the directories.
What I want to do now is to execute an .exe program using the directory settings from the config file.
Here is a piece of my code:
int main(){
ConfigFile cfg("htbaseconfig.properties");
bool exists = cfg.keyExists("backuplocation");
exists = cfg.keyExists("logdir");
exists = cfg.keyExists("execdir");
exists = cfg.keyExists("fulldir");
exists = cfg.keyExists("incdir");
exists = cfg.keyExists("appdir");
std::string bkploc = cfg.getValueOfKey<std::string>("backuplocation");
std::cout << "Backup Location: " << bkploc << "\n";
std::string bkplogdir = cfg.getValueOfKey<std::string>("logdir");
std::cout << "Log Location: " << bkplogdir << "\n";
std::string bkpexec = cfg.getValueOfKey<std::string>("execdir");
std::cout << "Exec Directory: " << bkpexec << "\n";
std::string bkpfulldir = cfg.getValueOfKey<std::string>("fulldir");
std::cout << "Full Directory: " << bkpfulldir << "\n";
std::string bkpappdir = cfg.getValueOfKey<std::string>("appdir");
std::cout << "Real app Directory: " << bkpappdir << "\n\n\n";
for( ; ; ) {
Sleep(6000);
ShellExecute(NULL, L"open", , L"C:\\teste.htm", NULL,SW_SHOWNORMAL);
}
std::cin.get();
return (0);}
Inside the ShellExecute, I wanted to execute the following line parsing the config options:
$execdir/program.exe $logdir/log.txt $bkpappdir $bkploc
How do I do this? I want to execute my program with the variables I get on std::cout.
You must pass to ShellExecute, instead of the second NULL, a string (c string, a char[]) that contains all parameters, like if you are passing them to the command line.
So Will be something like
ShellExecute(NULL, L"open", , L"C:\\teste.htm", "option=param option2=param2",SW_SHOWNORMAL);
Depends on how you parse them (or how they are parsed) from the other exe file