C语言中打开一个应用程序可以调用或运行命令system(),也可以调用操作系统的API函数,比如Windows系统可以调用CreateProcess()、ShellExecuteEx()等函数来打开一个应用程序。
举例
#include
#include
int main()
{
printf("在windows上打开记事本程序\n");
system("notepad.exe");
return 0;
}
默认路径是源文件的路径。 如果指定别的路径,两种方法: 一是相对当前默认路径指定路径。 二是指定绝对路径。 譬如: E:\C\c.c 这是源文件 E:\C\abc.exe E:\D\abcd.exe C:\A\aaa.exe 如果想执行abc.exe,那么可以不指定路径:
#include
int main()
{
char *cmd="abc.exe";
system(cmd);
return 0;
}
如果想执行abcd.exe,那么可如下指定路径:
#include
int main()
{
char *cmd="abc.exe";
system(cmd);
return 0;
}
对于空格问题,可按如下方法解决
#include
int main()
{
char *cmd="E:\\AB\" \"CD\\xyz.exe";
//即执行E:\AB CD\xyz.exe
system(cmd);
return 0;
}
转载自:http://bbs.sunsili.com/forum.php?mod=viewthread&tid=6#lastpost