Path: utzoo!attcan!uunet!wuarchive!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!haven!adm!news From: mchinni@pica.army.mil (Michael J. Chinni, SMCAR-CCS-E) Newsgroups: comp.unix.internals Subject: Re: System() function Message-ID: <24829@adm.BRL.MIL> Date: 22 Oct 90 22:00:23 GMT Sender: news@adm.BRL.MIL Lines: 30 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 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@pica.army.mil "System been down long?" () UUCP: ...!uunet!pica.army.mil!mchinni /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/