Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I store stdout value from system(); into a char* variable? 1

Status
Not open for further replies.

everyoneca

Programmer
Aug 2, 2005
3
0
0
CA

how do I store stdout value from system(); into a char* variable?

e.g.

I have child process that executes system(); function, but the output is displayed on server binary tty,

I would like to store the output of system("ls -la");

into a char* variable for later use / display .



is this possible ?

thanks,

taylor
 
Try "popen" instead of "system". It allows you to execute a process and have it's output appear as a file that you can read.



Trojan.
 

can you provide a short code example of popen(); storing the stdout value of "ls -la" or "netstat" into a char* type or string variable?

thankyou for your help

 
I may have found an example:

#include <stdio.h>

main()
{
FILE *fp;
char line[130];/* line of data from unix command*/

fp = popen("ls -l", "r");/* Issue the command.*/

/* Read a line*/
while ( fgets( line, sizeof line, fp))
{
printf("%s", line);
}
pclose(fp);
}

----
 
It seems the simplest way: use output redirection in cmd string:
Code:
system("myprog >myprogstdout.txt");
Now read (and don't forget to delete;) a file...
 
Why waste disk space and have to worry about not treading on other files when "popen" gives you an open file handle to the stream directly?



Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top