• C

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.
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");
   done = findfirst("*.*",&ffblk,0);
   while (!done)
      printf("  %s\n", ffblk.ff_name);
      done = findnext(&ffblk);
   return 0;
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
ico2Author Commented:
when i run that i get this error:
lexical: unknown escape sequence `\c'
'("a:\command.com /c dir")'
Get Certified for a Job in Cybersecurity

Want an exciting career in an emerging field? Earn your MS in Cybersecurity and get certified in ethical hacking or computer forensic investigation. WGU’s MSCSIA degree program was designed to meet the most recent U.S. Department of Homeland Security (DHS) and NSA guidelines.  

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

To avoid the escape sequence.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.