Solved

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

Posted on 2004-04-06
7
622 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:ico2
  • 5
  • 2
7 Comments
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10763910
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
 
LVL 9

Accepted Solution

by:
ankuratvb earned 20 total points
ID: 10763934
/* 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
 
LVL 1

Author Comment

by:ico2
ID: 10763936
when i run that i get this error:
lexical: unknown escape sequence `\c'
'("a:\command.com /c dir")'
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 9

Expert Comment

by:ankuratvb
ID: 10763938
The above example is for dos.U have findfirst() and findnext() fns. in other platforms as well.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10763943
Do this to search for files:
done = findfirst("find.txt",&ffblk,0);
0
 
LVL 1

Author Comment

by:ico2
ID: 10763959
thanks for the help ankuratvb. i now do not need to use command.com in my os.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10763979
U'll have to use:
system("c:\\command.com /c dir");

To avoid the escape sequence.
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question