turbo c & batch file
My batch file is executing with c program but when I used start notepad.exe in batch file
it shows bad command .But when I execute my bat开发者_JAVA技巧ch file individully it works perfect.What's
the reason?
Could you please post your bat file? It could be that the path to notepad.exe is relative to where you are running your bat file when you execute it manually, but when you call it from your C application you are running it from the C executable's location so it no longer finds either notepad.exe or a fiel that you may be passing to notepad.exe
It must have to do with the path of your notepad.exe ,if you are on windows then try to add the path of notepad.exe to environment variable "PATH",that might simplifies lots of things.
you say that you execute the batch file from the C program. are you sure the environment is properly setup when you start executing the batch file ?
if the PATH environment variable is not correctly set when executing the batch file, then the batch file will not execute. but the batch file will work without problem when launched from the command line, because the environment is correctly set in this case.
start
is an internal command of cmd.exe
; it is not a program by itself. To run start
you need to run cmd.exe
and have that cmd.exe
do the start
thing.
The system()
C function executes a shell (likely cmd.exe
in Windows) and passes the argument to that shell -- and start
"works".
The exec*
functions do not load a shell -- and start
by itself "don't work": it needs to be 'inside' cmd.exe
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
// system "works"
system("start C:\\tmp");
// this don't work
execl("start", "start", "C:\\Windows", (char*)0);
printf("Oops: execl with start failed\n");
// this "works"
execl("C:\\Windows\\System32\\cmd.exe", "start", "/c", "start", "C:\\Windows\\", (char*)0);
printf("Oops: execl with <path>\\cmd.exe failed\n");
return 0;
}
精彩评论