unix commands in c++

Discussion in 'C++' started by blackbug, Aug 21, 2010.

  1. blackbug

    blackbug Guest

    I am trying to write a small code which uses unix commands
    i am using the system() function call to execute the commands.

    but this will just execute them and would return back true or false,
    what if i need to feed the output generated by executing command in
    the variables of my c++ program?

    example:
    system("ls | wc -l");

    this would return the number of output rows, for eg say number of rows
    are 6.
    and now i want to use this number '6' as a value to any variable in my
    c++ code.

    Please let me know if anyone know solution for this.
    Also, I was looking for some c++/c implementation of some unix
    commands, if we can find them.

    Thanks

    regards,
    KK
     
    blackbug, Aug 21, 2010
    #1
    1. Advertising

  2. blackbug wrote:
    > I am trying to write a small code which uses unix commands
    > i am using the system() function call to execute the commands.
    >
    > but this will just execute them and would return back true or false,
    > what if i need to feed the output generated by executing command in
    > the variables of my c++ program?
    >
    > example:
    > system("ls | wc -l");
    >
    > this would return the number of output rows, for eg say number of rows
    > are 6.
    > and now i want to use this number '6' as a value to any variable in my
    > c++ code.


    Look at the POSIX popen() and pclose() functions. popen() is like
    system() but gives you a stream from which you can read the output of
    the command, as text. When you are done, you should close the stream
    with pclose().

    --
    Niklas Holsti
    Tidorum Ltd
    niklas holsti tidorum fi
    . @ .
     
    Niklas Holsti, Aug 21, 2010
    #2
    1. Advertising

  3. blackbug

    Rolf Magnus Guest

    blackbug wrote:

    > Also, I was looking for some c++/c implementation of some unix
    > commands, if we can find them.


    A lot of those could be found on gnu.org. Search e.g. for the coreutils
    package.
     
    Rolf Magnus, Aug 22, 2010
    #3
  4. blackbug

    blackbug Guest

    thanks to both of you
    it was really helpful
     
    blackbug, Aug 22, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yadagiri Rao KP

    Run unix commands from Java

    Yadagiri Rao KP, Aug 5, 2003, in forum: Java
    Replies:
    2
    Views:
    7,838
    Dave Miller
    Aug 5, 2003
  2. Yadagiri Rao KP

    Get all commands from unix

    Yadagiri Rao KP, Aug 7, 2003, in forum: Java
    Replies:
    4
    Views:
    430
    William Dowling
    Aug 7, 2003
  3. Spendius
    Replies:
    2
    Views:
    2,975
    Rogan Dawes
    Dec 13, 2004
  4. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    3,974
    Thomas Stegen
    Jun 28, 2003
  5. Tim Stanka
    Replies:
    1
    Views:
    804
    Jeff Epler
    Aug 2, 2004
Loading...

Share This Page