473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.
Jul 19 '05 #1
3 29790
igthibau wrote:
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++


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).
Jul 19 '05 #2
igthibau wrote:

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 ?


#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
rg********@pobox.com
Remove the 'g' from my address to send me mail.

Jul 19 '05 #3

"igthibau" <ig******@wanadoo.fr> wrote in message
news:bl**********@news-reader1.wanadoo.fr...
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++


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

-Mike
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Birch | last post by:
I have a python script that uses the print function throughout, and as well uses calls to os.system() to spawn DOS commandline executables. My issue is that I redirect all of the output from this...
12
by: Gil | last post by:
I am running a C++ process on Solaris that needs to find out how much diskspace is free and used on the system. Is 'system' in stdlib.h the only way to make OS calls from C++? In this case,...
8
by: Peter Olcott | last post by:
Is there any way to intercept system calls to windows such as TextOut and DrawText? what kinds of system calls can be intercepted, and what is the mechanism to accomplish this?
5
by: markus | last post by:
Hi, I have a question that deals with the standard c library VS (Unix) system calls. The question is: which header files (and functions) are part of the C library and which header files (and...
22
by: markus | last post by:
Hi, There are more than 1000 defined system calls in the Unix standard specification, however, a majority of them are optional and the availability of system calls are dependent on the OS...
11
by: talk | last post by:
hi,guy i have a question. are the functions in <stdio.h> system calls provided by operation system? if so, i want to know how C implements that we can call system calls by using the functions in...
21
by: omkar pangarkar | last post by:
Hi all, I have two simple hello world programs one using printf() and other using write() --prog 1-- #include<stdio.h> #include<stdlib.h> int main() { printf("Hello"); /* up to here...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.