How to capture process output in C?

Is there any analog of PHP's system in C?

man system says, that system return status of the command, but I need the output (like in PHP).

Of course, I can use pipes for this, but is there any standard way?

Answers


You can make use of popen and related function as:

// command to be run.
char *cmd = "date"; 

// open pipe stream.
FILE *fp = popen(cmd,"r");
int ch; 

// error checking.
if(!fp) {
        fprintf(stderr,"Error popen with %s\n",cmd);
        exit(1);
}   

// read from the process and print.
while((ch = fgetc(fp)) != EOF) {
        putchar(ch);
}

// close the stream.
pclose(fp);

Ideone link


If you need the output of the command, you'd use popen() on Unix (with "r" to indicate that you want to read from the command).

FILE *fp = popen("some -convoluted command", "r");
...check for validity...
...read data from command...
pclose(fp);

Need Your Help

jetty 7 continuations

java jetty continuations

i'm using jetty-7.4.1.v20110513 and servlet-api-2.5

What is wrong when i try to post String with scandinavian characters from Android to Php, like ÅÄÖ

java php android post http-accept-language

I can't find any reason why my scandinavian characters passed to php isn't shown and stored correctly.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.