By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Executing a process within c

P: n/a
How do I programatically run a dos command like 'dir' (I assume this will be
the same way as running normal files, which I'd like to know how to do too).
Also, will the output from this go into stdout?

Is there any better way to get a list of files in a directory than parsing a
'dir' ?

Thanks,
J
Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Jeremy" <jo******@n0sp4mhotmail.com> writes:
How do I programatically run a dos command like 'dir' (I assume this will be
the same way as running normal files, which I'd like to know how to do too).
This is a FAQ.

19.27: How can I invoke another program (a standalone executable,
or an operating system command) from within a C program?

A: Use the library function system(), which does exactly that.
Note that system's return value is at best the command's exit
status (although even that is not guaranteed), and usually has
nothing to do with the output of the command. Note also that
system() accepts a single string representing the command to be
invoked; if you need to build up a complex command line, you can
use sprintf(). See also question 19.30.

References: K&R1 Sec. 7.9 p. 157; K&R2 Sec. 7.8.4 p. 167,
Sec. B6 p. 253; ISO Sec. 7.10.4.5; H&S Sec. 19.2 p. 407; PCS
Sec. 11 p. 179.
Also, will the output from this go into stdout?
Normally.
Is there any better way to get a list of files in a directory than parsing a
'dir' ?


This is also a FAQ.

19.20: How can I read a directory in a C program?

A: See if you can use the opendir() and readdir() functions, which
are part of the POSIX standard and are available on most Unix
variants. Implementations also exist for MS-DOS, VMS, and other
systems. (MS-DOS also has FINDFIRST and FINDNEXT routines which
do essentially the same thing.) readdir() only returns file
names; if you need more information about the file, try calling
stat(). To match filenames to some wildcard pattern, see
question 13.7.

References: K&R2 Sec. 8.6 pp. 179-184; PCS Sec. 13 pp. 230-1;
POSIX Sec. 5.1; Schumacher, ed., _Software Solutions in C_
Sec. 8.
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Nov 14 '05 #2

P: n/a
"Jeremy" <jo******@n0sp4mhotmail.com> writes:
How do I programatically run a dos command like 'dir' (I assume this will be
the same way as running normal files, which I'd like to know how to do too).
Also, will the output from this go into stdout?
Use the system() function, e.g.:

system("dir");

The output of the "dir" command will normally go to stdout.
Is there any better way to get a list of files in a directory than parsing a
'dir' ?


There are a number of better ways, but none of them are portable (the
C standard has no concept of directories). You'll need to ask in a
newsgroup that's specific to your operating system.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3

P: n/a
Thanks, most helpful, where can I get my hands on the whole FAQ?

J
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@benpfaff.org...
"Jeremy" <jo******@n0sp4mhotmail.com> writes:
How do I programatically run a dos command like 'dir' (I assume this will be the same way as running normal files, which I'd like to know how to do too).

This is a FAQ.

19.27: How can I invoke another program (a standalone executable,
or an operating system command) from within a C program?

A: Use the library function system(), which does exactly that.
Note that system's return value is at best the command's exit
status (although even that is not guaranteed), and usually has
nothing to do with the output of the command. Note also that
system() accepts a single string representing the command to be
invoked; if you need to build up a complex command line, you can
use sprintf(). See also question 19.30.

References: K&R1 Sec. 7.9 p. 157; K&R2 Sec. 7.8.4 p. 167,
Sec. B6 p. 253; ISO Sec. 7.10.4.5; H&S Sec. 19.2 p. 407; PCS
Sec. 11 p. 179.
Also, will the output from this go into stdout?


Normally.
Is there any better way to get a list of files in a directory than

parsing a 'dir' ?


This is also a FAQ.

19.20: How can I read a directory in a C program?

A: See if you can use the opendir() and readdir() functions, which
are part of the POSIX standard and are available on most Unix
variants. Implementations also exist for MS-DOS, VMS, and other
systems. (MS-DOS also has FINDFIRST and FINDNEXT routines which
do essentially the same thing.) readdir() only returns file
names; if you need more information about the file, try calling
stat(). To match filenames to some wildcard pattern, see
question 13.7.

References: K&R2 Sec. 8.6 pp. 179-184; PCS Sec. 13 pp. 230-1;
POSIX Sec. 5.1; Schumacher, ed., _Software Solutions in C_
Sec. 8.
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms

Nov 14 '05 #4

P: n/a
"Jeremy" <jo******@n0sp4mhotmail.com> writes:
Thanks, most helpful, where can I get my hands on the whole FAQ?


What, is Google down?
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #5

P: n/a
"Jeremy" <jo******@n0sp4mhotmail.com> writes:
Thanks, most helpful, where can I get my hands on the whole FAQ?


Google "C FAQ"; it's the first link.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6

P: n/a
In article <40******@news.comindico.com.au>, jo******@n0sp4mhotmail.com says...
Thanks, most helpful, where can I get my hands on the whole FAQ?


This link might be exactly what you need:

http://www.justfuckinggoogleit.com/

Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.