Dasser Basyouni Dasser Basyouni - 1 year ago 96
C++ Question

output Command Shell in if statment

I have searched many and read many codes but I do not understand why is the variable(the cmd output) can not be printed to be sure of it
the question might be repetitive but I can not comment on any code I saw as I am below 50 reputation
here is the code I am trying and I am trying to get the output of the cmd and use it in IF statement so of example if it gives error then resend the command

#include <stdlib.h>
#include <string>
#include <iostream>
#include <stdio.h>

int main(int argc, char *argv[])
FILE *lsofFile_p = popen("adb reboot", "r");

if (!lsofFile_p)
return -1;

char buffer[1024];
char *line_p = fgets(buffer, sizeof(buffer), lsofFile_p);
printf("\n\n", *lsofFile_p);

edited photo
enter image description here

Answer Source
printf("\n\n", *lsofFile_p);

This only prints two \n. That's it. Not more, not less. That's why you think that it prints "nothing". Also note that *lsofFile_p is undefined behavior because you closed the file the line before, and this invalidates the file pointer.

You probably want to print the line that you read from lsofFile_p, which you stored in buffer:

//'%s' take the second argument of printf and interprets it as a string
printf("%s\n\n", buffer);

Consider using std::string, std::cout, and other facilities to make your life easier, you don't have to program in C when you are programming in C++.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download