Joining stdout and stderr: 2>&1

Needed to get stderr with popen:

/*
* Purpose: Program to demonstrate the popen function.
*
* to do: Check that the ‘popen’ was successfull.
*
* Author: M J Leslie.
* Date: 08-Jan-94
*/

#include

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);
}

– popen will always execute the command from within the Bourne shell. – popen feeds the STDOUT back to your program. If you want STDERR, the following will do the trick.

fp=popen(“ls -l 2>&1”, “w”);
AAAA
||||

From:
http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/popen.html

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s