Re: command output to variables

"Michele Mondelli" <[email protected]>
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <[email protected]>
A simple example of using pipe in Linux.

Regards


--
Homepage: http://www.mithenks.com
Slackware GNU/Linux User since 2003
Registered Linux User #383379
Registered Linux Machine #391361
pipe.c (text/x-csrc, 439 B)
/*
 * Run the 'ls' command and print the output to stdout.
 * 
 * Compile with 
 * 		gcc -Wall pipe.c -o pipe
 * 
 * 
 * Mithenks <[email protected]>
 * 
 */
#include <stdio.h>
#include <stdlib.h>

int main() {
	
	FILE *pipe;
	char buffer[1024];
	
	if ( (pipe = popen("ls","r") ) < 0 ) {
		perror("popen(): ");
		exit(-1);
	}
	
	
	fread(buffer,sizeof(buffer),1,pipe);
	printf("Output:\n%s\n",buffer);
	
	pclose(pipe);
	
	return 0;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.