• 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.
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
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.  

 
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
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.