Connecting Tech Pros Worldwide Help | Site Map

system calls

igthibau
Guest
 
Posts: n/a
#1: Jul 19 '05
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
Guest
 
Posts: n/a
#2: Jul 19 '05

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).
Russell Hanneken
Guest
 
Posts: n/a
#3: Jul 19 '05

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.



Mike Wahler
Guest
 
Posts: n/a
#4: Jul 19 '05

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


Closed Thread