I have a binary file which prints the result instead of returning the value, if I execute it using cmd I am getting printed text, I managed to execute it from C code but it seems like I can not get the text it usually prints to be stored in a variable I can use later for further decisions. I do not have that much of experience in C and I googled a lot. I came across the idea of using clip but my cmd is saying that clip command can not be found. any help or ideas would be appreciated.
-
On POSIX systems, use `popen()`. – Jonathan Leffler Apr 27 '17 at 10:52
-
it did not work, I think popen reads the output (returned value) but the binary file I have is just using printf() – Yahya Hussein Apr 27 '17 at 10:53
-
2I think you misused `popen()` then, as one of its two purposes in life is to let the invoking program read the standard output of the invoked program. (The other purpose is to let the invoking program write to the standard input of the invoked program.) – Jonathan Leffler Apr 27 '17 at 10:56
-
[An old answer of mine](http://stackoverflow.com/questions/3736210/how-to-execute-a-shell-script-from-c-in-linux/3736484#3736484) may be of help. – pmg May 08 '17 at 08:05
4 Answers
You can use the system
function from <stdlib.h>
to run the command you want. To get the command's output, you modify your command like in this question to save the command's output to a file. Then you can use the file I/O functions in <stdio.h>
to process the command output.
-
thanks, it seems like this binary files uses printing functions instead of returning the value. I tried your solution before and it did not work for me :( – Yahya Hussein Apr 27 '17 at 10:50
In Linux, you may do command substitution and pass its result as arguments to the program, Something like this
./your_program "$(/path/to/your/binary/file)"
Suppose your main is
int main(int argc,char* argv[]){
.
.
return 0;
}
Acess the arguments like argv[1]
and so.
Here the $(command)
does the substitution and it passes the printed values from the binary as arguments to the pgm. Hope this helps.

- 21,411
- 5
- 55
- 102
Use snprintf function. For e.g.
snprintf(cmdbuff, BUFFER_LEN, "dmidecode --type 17 | grep -i Size | grep -o '\\<[0-9]*\\>' | paste -sd+ | bc");
Here cmdbuff is character array where command will be stored , BUFFER_LEN is a size of the character array
Then use popen and fgets to get the output of command into some buffer as shown below
if((fd = popen(cmdbuff,"r")) != NULL)
{
fgets(buffer, BUFFER_LEN, fd);
sprintf(vnfc_configured_memory, "%s", buffer);
vnfc_configured_totalRAM = atof(vnfc_configured_memory);
}

- 1,251
- 1
- 13
- 24
The correct function pair to use on POSIX systems is popen()
and
pclose()
. You can perhaps use Microsoft's _popen()
and
_pclose()
unless the warning 'This API cannot be used in applications that execute in the Windows Runtime' matters to you.
You would use it more or less like this. I've had to invent the name of the command you wish to execute since the question doesn't specify that. I chose ./example.exe
as the name — and I'm assuming it needs no arguments.
char cmd[] = "./example.exe";
FILE *fp = popen(cmd, "r");
if (fp != NULL)
{
char buffer[4096];
size_t nbytes;
while ((nbytes = fread(buffer, sizeof(buffer), sizeof(char), fp)) != 0)
{
…process nbytes of data…
…it is not a null-terminated string unless you add the null byte…
}
pclose(fp);
}
else
{
…report error for failure to execute command…
}

- 730,956
- 141
- 904
- 1,278