Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dos commands

Status
Not open for further replies.

KyoAD

Technical User
Aug 5, 2002
106
0
0
SG
Hi,

How do i call dos commands/programs in a C program? For example, to run 'dir' in the program? Can i do this by using the shell command? What libraries must i include?

 
Here's one way
Code:
#include <stdlib.h>

system( "dir" );

--
 
Actually if you wanna be much more precise you have to do:
Code:
#include <stdlib.h>
system( "command.com /c dir" );

Not sure it will work without the command.com... Have not tried with C (partly because I'm lazy) but in Tcl/Tk you have to specify command.com /c when it comes to internal commands such as dir
 
Code:
system ( "dir" );
will work but....
If you want to create a list of files for processing inside your code then capturing the output of dir is not straingtforward. In fact I've seen
Code:
system ( "dir > \\temp\\flist" );
if ( ( fHandle = fopen ( "\\temp\\flist", "r" ) ) )
  {
  while ( fgets ( szBuffer, sizeof (szBuffer), fHandle ) )
  .....
  ....
which is barbaric but works
If you need a list of files the look at opendir and readdir
 
> If you need a list of files the look at opendir and readdir
Agreed.
Though the OP probably wants the DOS/Windows equivalents of findfirst and findnext.

However, which spelling of findfirst you should use seems dependent on your compiler, so some digging may be in order.

--
 
I generally do not program under windows.
however, can you not use channel redirection like u would on unix instead of > into a file and then read it?
 
Well
Code:
FILE *fp = popen( "dir", "r" );
while ( fgets ( buff, sizeof (buff), fp ) ) {
}
pclose( fp );
Gets around the temporary file problem, but it needs the POSIX libraries installed for it to work on windows.
It almost certainly isn't available in DOS.

--
 
yeah that is what I meant...
sorry for the bad explainations...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top