Thread: C++ and Shell

  1. #1
    Registered User tekhawk's Avatar
    Join Date
    Aug 2006
    Location
    USA Ca
    Posts
    25

    Question C++ and Shell

    what i want to do is have my c++ program issue shell commands or access my linux command line

    seeing as to how im new to c++ and ive been using linux for a long time this would let me make powerful c++ programs for linux while im still learning more functions and the linux c++ functions

  2. #2
    Registered User tekhawk's Avatar
    Join Date
    Aug 2006
    Location
    USA Ca
    Posts
    25
    also whats a good source for linux geared c++ guides

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    system() is probably the call you're after. Although many of the commands on the *nix command line have equivalent functions
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Registered User tekhawk's Avatar
    Join Date
    Aug 2006
    Location
    USA Ca
    Posts
    25
    i tried the system(); function last night using

    system("finger");

    and/or

    system('finger');

    could someone show me a sample so i can figure out where i went wrong

    or what do i need to include to uses this command

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    stdlib.h is what the man page says... man is good.... you're on linux right?

    :> man system
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    <cstdlib> in C++.

    Sample:
    Code:
    #include <cstdlib>
    
    int main() {
        system("ls");
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >system("ls");
    std::system("ls");
    My best code is written with the delete key.

  8. #8
    Registered User tekhawk's Avatar
    Join Date
    Aug 2006
    Location
    USA Ca
    Posts
    25
    thank you much

    dwks you saved me from using the c header FYB wanted me to use

    does anyone know where i can get info on c++ linux functions

    so that i dont havve to totally rely on shell

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    > dwks you saved me from using the c header FYB wanted me to use

    my evil plot was foiled again!!!!
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    Registered User tekhawk's Avatar
    Join Date
    Aug 2006
    Location
    USA Ca
    Posts
    25
    lol

    i think im going to like this forum

    thanks for the try FYB

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    >system("ls");
    std::system("ls");
    I should have thought of that . . . I compiled it and everything . . .
    Code:
    $ g++ systemls.cpp -o systemls
    $
    does anyone know where i can get info on c++ linux functions
    Well, you might try a book, such as one recommended in the recommended C++ books sticky. You might try searching google, eg "c++ function getline". You might try an online reference such as cppreference.com.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User tekhawk's Avatar
    Join Date
    Aug 2006
    Location
    USA Ca
    Posts
    25
    would you guys mind explaning that last bit of code

    Code:
    >system("ls");
    std::system("ls");

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >would you guys mind explaning that last bit of code
    The functions that C++ inherited from C are wrapped in the std namespace. Just like you need to qualify cout with std::, you need to qualify something like printf with std:: as well.
    My best code is written with the delete key.

  14. #14
    Registered User tekhawk's Avatar
    Join Date
    Aug 2006
    Location
    USA Ca
    Posts
    25
    is
    Code:
    >system("ls");
    the same as
    Code:
    std::system("ls");

  15. #15
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    which would also call the C version that offered up, although you're supposed to use a C++ header file. That's kindof like a rule... or something.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed