initialize a string variable with output of a command?

Discussion in 'C++' started by Glen, Sep 27, 2003.

  1. Glen

    Glen Guest

    Hi,

    I just have a quick question
    Im trying to initialize a string (or char) variable with the information
    created by the output of a shell (or DOS) command..

    Example:
    I want to execute a command such as
    system("cat /test.txt | grep something ") or whatever DOS or linux command
    and assign the output of that command to the variable str;

    so I tried
    char str[128]="system(\"my_command\")";

    then when I do, cout<<str;
    I get system(\"my_command\"),
    which is the acctual command, and not its output

    Is there any way I can do it?

    Thank you
     
    Glen, Sep 27, 2003
    #1
    1. Advertisements

  2. Glen wrote:
    > Hi,
    >
    > I just have a quick question
    > Im trying to initialize a string (or char) variable with the information
    > created by the output of a shell (or DOS) command..


    There is no standard way. Try a group that discusses your particular
    platform & C++ implementation.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Sep 27, 2003
    #2
    1. Advertisements

  3. Glen

    Jerry Coffin Guest

    In article <EGndb.67483$-kc.rr.com>,
    says...
    > Hi,
    >
    > I just have a quick question
    > Im trying to initialize a string (or char) variable with the information
    > created by the output of a shell (or DOS) command..


    [ ... ]

    > Is there any way I can do it?


    None is included in the C++ standard, but if you ask about popen in a NG
    specific to the OS you're using, there's a pretty fair chance somebody
    will be able to help you.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Sep 27, 2003
    #3
  4. Glen

    David Rubin Guest

    Glen wrote:
    >
    > Hi,
    >
    > I just have a quick question
    > Im trying to initialize a string (or char) variable with the information
    > created by the output of a shell (or DOS) command..
    >
    > Example:
    > I want to execute a command such as
    > system("cat /test.txt | grep something ") or whatever DOS or linux command
    > and assign the output of that command to the variable str;
    >
    > so I tried
    > char str[128]="system(\"my_command\")";
    >
    > then when I do, cout<<str;
    > I get system(\"my_command\"),
    > which is the acctual command, and not its output
    >
    > Is there any way I can do it?


    Try

    system("my_command > command.out");
    string s;
    ifstream ifs("command.out");
    std::getline(ifs, s);
    ifs.close();

    /david

    --
    Andre, a simple peasant, had only one thing on his mind as he crept
    along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
    -- unknown
     
    David Rubin, Sep 29, 2003
    #4
    1. Advertisements

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. Earl Eiland
    Replies:
    3
    Views:
    565
    Peter Hansen
    May 16, 2005
  2. Trent Mick
    Replies:
    0
    Views:
    509
    Trent Mick
    May 16, 2005
  3. TP
    Replies:
    1
    Views:
    729
    Gabriel Genellina
    Oct 27, 2008
  4. sin kanti
    Replies:
    5
    Views:
    222
    James Edward Gray II
    Apr 3, 2005
  5. championsleeper
    Replies:
    8
    Views:
    628
Loading...

Share This Page