Like Tree1Likes
  • 1 Post By Elkvis

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

This is a discussion on Beginner's question: how to cin after system() within the C++ Programming forums, part of the General Programming Boards category; Hello! Imagine I want to have my program run a shell script and I want to use its return in ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,289
    there is also an unofficial boost library that lets you do that sort of thing in a more C++-like way.
    Elysia likes this.
    Code:
    namespace life
    {
        const bool change = true;
    }

  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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21