Solved

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

Posted on 2004-04-06
7
624 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

636 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