vilva@csvax.csc.lsu.edu (Vilva Natarajan) (10/23/90)
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. Please advice. vilva@csvax.csc.lsu.edu
jik@athena.mit.edu (Jonathan I. Kamens) (10/23/90)
I fail to see how this has anything to do with Unix internals. It is clearly a question about programming under Unix, which means that it clearly belongs in comp.unix.programmer. I've cross-posted to comp.unix.programmer and directed followups there. In article <24826@adm.BRL.MIL>, vilva@csvax.csc.lsu.edu (Vilva Natarajan) writes: |> 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. Use popen("man cat | wc -c", "r"), which will return a FILE *, and you can read the output of wc from the FILE *. See popen(3). -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710
mchinni@pica.army.mil (Michael J. Chinni, SMCAR-CCS-E) (10/23/90)
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@pica.army.mil "System been down long?" () UUCP: ...!uunet!pica.army.mil!mchinni /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
olav@nososl.UUCP (Olav Eide) (10/24/90)
In article <24826@adm.BRL.MIL> vilva@csvax.csc.lsu.edu (Vilva Natarajan) writes: > >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. >Please advice. > Why not use the popen call ? fd = popen("man cat | wc -c","r"); fgets(line,LL,fd); sscanf(line,"%d",&your_variable); You should of course check the return values for popen and fgets
sparks@power.viewlogic.com (Alan Sparks) (10/24/90)
In article <24826@adm.BRL.MIL> vilva@csvax.csc.lsu.edu (Vilva Natarajan) writes: > >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. >Please advice. > >vilva@csvax.csc.lsu.edu Use popen(1) and pclose(1). Try something more like this: { char retval[100]; FILE *pp; pp = popen("man cat |wc -c","r"); fscanf(pp,"%s",retval); pclose(pp); } -- Alan Sparks voice: (508) 480-0881 Internet: sparks@viewlogic.com VIEWlogic Systems Inc., 293 Boston Post Rd. W., Marlboro MA 01752 Disclaimer: VIEWlogic didn't say this; I might have. Who wants to know?
chapman@sco.COM (Brian Chapman) (10/26/90)
vilva@csvax.csc.lsu.edu (Vilva Natarajan) writes: >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. >Please advice. Try looking in to popen() rater than system(). popen() is the same thing except you get pipes to and from the command. -- Brian Chapman uunet!sco!chapman Pay no attention to the man behind the curtain!
boyd@necisa.ho.necisa.oz (Boyd Roberts) (11/05/90)
In article <8419@scolex.sco.COM> chapman@sco.COM (Brian Chapman) writes: >Try looking in to popen() rater than system(). popen() >is the same thing except you get pipes to and from the >command. > That's pipe, singular; to _or_ from. It's uni-directional. Boyd Roberts boyd@necisa.ho.necisa.oz.au ``When the going gets wierd, the weird turn pro...''