newbie ?: how do i run external .exe and com files from a c program

how do i run external .exe and com files from a c program. i also need to use switches when running these. so for instance i need to run a program like this "c:\command.com /c dir"
also how do i find out if a file or directory exists in c.
LVL 1
ico2Asked:
Who is Participating?
 
ankuratvbConnect With a Mentor Commented:
/* findfirst and findnext example */

#include <stdio.h>
#include <dir.h>

int main(void)
{
   struct ffblk ffblk;
   int done;
   printf("Directory listing of *.*\n");
   //system("cd\");
   done = findfirst("*.*",&ffblk,0);
   while (!done)
   {
      printf("  %s\n", ffblk.ff_name);
      done = findnext(&ffblk);
   }
   return 0;
}
0
 
ankuratvbCommented:
Hi, for running external programs ,u can use the system() command.

do this:
system("c:\command.com /c dir");

U can also use the execl(when u know how many separate arguments and execv(when u have variable arguments) functions

>how do i find out if a file or directory exists in c.
Use the findfirst() function
0
 
ico2Author Commented:
when i run that i get this error:
lexical: unknown escape sequence `\c'
'("a:\command.com /c dir")'
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
ankuratvbCommented:
The above example is for dos.U have findfirst() and findnext() fns. in other platforms as well.
0
 
ankuratvbCommented:
Do this to search for files:
done = findfirst("find.txt",&ffblk,0);
0
 
ico2Author Commented:
thanks for the help ankuratvb. i now do not need to use command.com in my os.
0
 
ankuratvbCommented:
U'll have to use:
system("c:\\command.com /c dir");

To avoid the escape sequence.
0
All Courses

From novice to tech pro — start learning today.