system calls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • igthibau

    system calls

    Hello one and all,
    Hopefully someone can help me out here. I am trying within a C / C++ program
    to invoke command line instructions (under linux) i.e.:
    say I wish to list the contents of the current dir I would type (in bash
    say) : ls (enter).
    Now, how do I do to enact this action within a code ?
    In fortran the appropriate command is : callsystem "ls", give or take
    syntaxic details.
    I wish to do the same in C / C++

    Any help appreciated, thanks

    G.T.
  • Dimitris Kamenopoulos

    #2
    Re: system calls

    igthibau wrote:
    [color=blue]
    > Hello one and all,
    > Hopefully someone can help me out here. I am trying within a C / C++
    > program to invoke command line instructions (under linux) i.e.:
    > say I wish to list the contents of the current dir I would type (in bash
    > say) : ls (enter).
    > Now, how do I do to enact this action within a code ?
    > In fortran the appropriate command is : callsystem "ls", give or take
    > syntaxic details.
    > I wish to do the same in C / C++[/color]

    Invoking "ls" is not a system call[*], it is just executing an external
    ("system") command. You can do it with system() in <cstdlib>.
    system("ls") will execute ls and return ls' exit code. See also the man page
    of system (man 3 system) and it might be a good idea to check for more
    advanced stuff in a linux (or unix) specific forum.
    [*] system calls under Unix-like OSes are all those strange functions
    documented in man(2).

    Comment

    • Russell Hanneken

      #3
      Re: system calls

      igthibau wrote:[color=blue]
      >
      > Hopefully someone can help me out here. I am trying within a C / C++
      > program to invoke command line instructions (under linux) i.e.:
      > say I wish to list the contents of the current dir I would type (in bash
      > say) : ls (enter).
      > Now, how do I do to enact this action within a code ?[/color]

      #include <cstdlib> // needed for std::system
      #include <iostream>
      #include <ostream>

      int main()
      {
      if (std::system(0) )
      {
      // A command processor is available.
      std::system("ls ");
      }
      else
      {
      std::cerr << "A command processor is not available.\n";
      }
      }

      --
      Russell Hanneken
      rghanneken@pobo x.com
      Remove the 'g' from my address to send me mail.



      Comment

      • Mike Wahler

        #4
        Re: system calls


        "igthibau" <igthibau@wanad oo.fr> wrote in message
        news:blpgit$ppq $1@news-reader1.wanadoo .fr...[color=blue]
        > Hello one and all,
        > Hopefully someone can help me out here. I am trying within a C / C++[/color]
        program[color=blue]
        > to invoke command line instructions (under linux) i.e.:
        > say I wish to list the contents of the current dir I would type (in bash
        > say) : ls (enter).
        > Now, how do I do to enact this action within a code ?
        > In fortran the appropriate command is : callsystem "ls", give or take
        > syntaxic details.
        > I wish to do the same in C / C++[/color]

        #include <cstdio>
        int main()
        {
        std::system("ls ");
        return 0;
        }

        -Mike


        Comment

        Working...