Thread: Shell output into program during runtime

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    13

    Shell output into program during runtime

    I'm writing a program that needs to call a shell command during runtime. The output of said command needs to come back into the program as a c++ string.

    At first I just used
    Code:
    system("echo Hello World > temp");
    string hello = getFile("temp");//where getFile simply returns the contents of temp as a string.
    However, this is horrendously slow and isn't practical. I want to redirect the output from shell directly into my program. I attempted
    Code:
    system("echo Hello World 1>&0");
    string hello;
    while(!cin.eof()){
         char c;
         cin.get(c);
         hello += c;
    }
    but this failed also.

    If anyone can help, it would be greatly appreciated. I have a basic knowledge of C++ but only a rough understanding of bash shells, and so may be missing something obvious.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Look at the popen() function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Location
    USA
    Posts
    1
    Where you get it???

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Where you get it???
    Did you go to the same school of literacy as your president?

    How about forming a real sentence of what you want to ask rather than just mashing your head into the keyboard and posting the result?

    Oh, and do it in another topic rather than hijacking some dead thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-05-2009, 09:42 AM
  2. Program to execute shell commands.
    By LiquidLithium in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2004, 12:22 PM
  3. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  4. Replies: 3
    Last Post: 01-08-2004, 09:43 PM
  5. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM