Executing a process within c

J

Jeremy

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
 
B

Ben Pfaff

Jeremy said:
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.
 
K

Keith Thompson

Jeremy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top