Solved

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

Posted on 2004-04-06
7
618 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
Comment Utility
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
Comment Utility
/* 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
Comment Utility
when i run that i get this error:
lexical: unknown escape sequence `\c'
'("a:\command.com /c dir")'
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 9

Expert Comment

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

Expert Comment

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

Author Comment

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

Expert Comment

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

To avoid the escape sequence.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now