Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The attached snippet works and "prints" to QtCreator "console".
I need to redirect its output as required ... for now to "result"...



COBOL
int RESULT = system(" echo q  | sudo -S hcitool info 98:D3:31:F8:39:33 ") ;
                          if(RESULT == 0)
                          {
                              text = " SUCCESS ";
                              text += "Elapsed time ";
                              text += QString::number(QET->elapsed());
                              text += "  mS ";
                              ui->textEdit->append(text);
                              qDebug() << text;
                              ui->textEdit->append(result);
                              qDebug() << result;
                          }


What I have tried:

C++
int RESULT = system(" echo q  | sudo -S hcitool info 98:D3:31:F8:39:33  > result") ;


Redirecting this way stopped the output from console and no value in "result " .

Adding "| > result " did not help/
Posted
Comments
Salvatore Terress 17-Feb-24 19:36pm    
...the question is about how to process "system" call...
(I want to learn something as I code...)
I do not know how (want) to option QtProcess with such complex setting.
Besides "system" is more KISS (IMHO)

Don't use system(). Use popen() : popen(3) - Linux manual page[^]
Alternatively QProcess should do what you want.
or boost::process() : Chapter 28. Boost.Process - 1.84.0[^]
 
Share this answer
 
To add to what k5054 said, the reason adding "> result" didn't work is that calling system starts a process which is external to your app - so it doesn't have access to your memory. Just because the name "result" is the same as a variable in your app doesn't mean the new process can modify your app in any way.
What adding it does is tell the new process to redirect the standard output stream to a file called "result" in the current working directory which is why it "stopped the output from console" but you couldn't see the results. You could have read the file to access it, but then you'd have to known when the process ended as the file won't be readable until then.
 
Share this answer
 
Comments
Salvatore Terress 18-Feb-24 8:28am    
Are you saying that "QString result" is a file , hence I need to treat (read) it as a file?
OK I did redirect to this "file"...and than lost the output to stdout AKA console..
but why using "| > " also removed the output to console?
OriginalGriff 18-Feb-24 9:22am    
1) No, QString result is a variable in your app. The process you started has no access to it - the redirection you add sends it to a file which just happens to have the same name; the file is nothing to do with your app!
2) Because | is OS-speak for "Pipe output of the app on the left to the app on the right" - it doesn't display anything because it fed the output to an app instead of the console.
k5054 18-Feb-24 10:13am    
My 5. Intuiting that the OP was trying to redirect the system() output into a variable is worth every point.
Salvatore Terress 18-Feb-24 12:51pm    
..so I cannot avoid to use "file "
and I can use both " | > "...
if "system" has no knowledge of "result"
how will it have knowledge if I use QFile resultFile;?
" | > resultFile "
Salvatore Terress 18-Feb-24 16:00pm    
Here is my attempt to use "popen"

char s[5000];
char cmd[64]="echo q | sudo -S hcitool info 98:D3:31:F8:39:33";
FILE* pip=popen(cmd,"r");
fgets(s,5000,pip);

qDebug() << s;
return;

result:
I no longer get "connected" response from remote Bluetooth device
I get "disconnected" after few seconds

my "output" is limited to ONE line
[sudo] password for nov25-1: Requesting information ...

using "system" it took over 20 seconds for completion (expected _)

it definitely looks as "popen" terminates BEFORE remote device is finished responding.

not sure how to fix this , perhaps I have to give up and use QProcess
Here is a sample of popen that works, and demonstrates that the output can be more than a single line.
C++
#include <stdio.h>
#include <stdlib.h>

int main(
	)
{
    char buffer[160];
    FILE *foo;
    foo = popen("ls -l", "r");
    while (fgets(buffer, 160, foo) != NULL)
    {
        printf("%s", buffer);
    }
    pclose(foo);
    printf("End of stream encountered\n");

	return 0;
}

You need to check (by running at the terminal) that the command you are using produces actual output.


BTW re your comment "I am not allowed to ask Qt questions", this is not true, and no one has told you that. All that we have said is that you should not post Qt questions in the C/C++ forum. If you post them here in Quick Answers then people will (possibly) answer them. Also, this question is not a Qt specific onem as it is actually about getting the output of a command in Linux. The suggestion from k5054 to use popen is not specific to Qt.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900