System() function

Michael J. Chinni, SMCAR-CCS-E mchinni at pica.army.mil
Tue Oct 23 08:00:23 AEST 1990


Vilva,

>I am trying to do system("man cat | wc -c") in my program, and i need
>to store the value returned by "wc" in a variable declared in my program.
>I dont want to redirect it to a file and then read from the file.

The way to do this is via a "popen". Small segment of code to do this follows:
#include <stdio.h>
main()
{	FILE	*pipe;
	int	chars;

	pipe=popen("man cat | wc -c", "r");
	fscanf(pipe, "%d", &chars);
	pclose(pipe);
}

This gets the result of "man cat | wc -c" and stores it in chars. This code
doesn't do any error checking (like on the value of pipe after the popen to
check the success of the popen; on the return status of the fscanf, or the
return status of the pclose).

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
			    Michael J. Chinni
              Simulation Techniques and Workplace Automation Team
	 US Army Armament Research, Development, and Engineering Center
 User to skeleton sitting at cobweb   () Picatinny Arsenal, New Jersey  
    and dust covered workstation      () ARPA: mchinni at pica.army.mil
      "System been down long?"        () UUCP: ...!uunet!pica.army.mil!mchinni
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



More information about the Comp.unix.internals mailing list