Connecting Tech Pros Worldwide Help | Site Map

system calls

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 06:47 PM
igthibau
Guest
 
Posts: n/a
Default 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.

  #2  
Old July 19th, 2005, 06:47 PM
Dimitris Kamenopoulos
Guest
 
Posts: n/a
Default 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).
  #3  
Old July 19th, 2005, 06:47 PM
Russell Hanneken
Guest
 
Posts: n/a
Default 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@pobox.com
Remove the 'g' from my address to send me mail.



  #4  
Old July 19th, 2005, 06:47 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: system calls


"igthibau" <igthibau@wanadoo.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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.