Thread: Beginner's question: how to cin after system()

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    11

    Beginner's question: how to cin after system()

    Hello!

    Imagine I want to have my program run a shell script and I want to use its return in my program. Could you please tell me how to read it back into my program?

    Thank you!

    Also, best practices suggestions are welcome.

    I tried this, but the program expects an user input, as you can imagine. (And I think it is because the shell script has already been piped to stdout by the time I am expecting to retrive it, right?)

    Code:
    #include <cstdlib>
    #include <iostream>
    
    int main() {
        system("cd ~; ls | wc -l");
        int wc;
        std::cin >> wc;
        std::cout << "Wc is " <<  wc;
        return 0;
    }
    Last edited by yaraeovento; 11-05-2013 at 04:22 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    34,313
    Read up on popen().
    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
    Oct 2006
    Posts
    3,261
    there is also an unofficial boost library that lets you do that sort of thing in a more C++-like way.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    Thank you so much for both answers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. System programming - Absolute beginner confused
    By std10093 in forum C Programming
    Replies: 21
    Last Post: 11-24-2012, 02:21 PM
  2. beginner's question
    By amarprasovic in forum C Programming
    Replies: 2
    Last Post: 05-09-2010, 09:24 AM
  3. Beginner question - please help
    By mo34 in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2008, 02:34 PM
  4. Beginner Question!
    By Jeff in forum C Programming
    Replies: 3
    Last Post: 09-01-2004, 02:30 PM
  5. beginner question
    By davidnj732 in forum C Programming
    Replies: 2
    Last Post: 02-17-2003, 08:45 PM

Tags for this Thread