Opening a batch file (.bat) in a C program?
How do I start running a batch fil开发者_运维知识库e (.bat) from my C program? I used
system("start /B omanam.bat");
but it's not working. How can I make .bat to open through C?
Drop the start
. It's a cmd.exe thing. Just run system("omanam.bat");
.
If your C executable program and batch file are in same directory then
system("batchfilename.bat arg1 arg2");
where arg1
and arg2
are the arguments for this batch file.
If the batch file is in another directory
system("f:\\bin\\batchfilename.bat arg1 arg2");
where arg1
and arg2
are the arguments for this batch file.
C code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Calling batch file doit.bat\n");
system("doit Hello. theansweris: 42");
printf("Press \'Enter\' to exit the program\n");
getchar();
return 0;
}
Batch file code:
@rem This is the batch file doit.bat
@echo.
@echo.
@echo.
@echo In doit.bat:
@echo.
@echo.
@echo.
@echo argument #1 is ^"%1^"
@echo argument #2 is ^"%2^"
@echo argument #3 is ^"%3^"
@echo.
@echo.
@echo Tttttthat's all, folks!
@echo.
@echo.
精彩评论