即使给定的文件在那里,_execl()也会返回-1和错误消息,比如“没有这样的文件或目录”。当我直接在命令提示符下运行gzip命令时,它可以工作。我不能理解我在这里遗漏了什么。
#include <stdio.h>
#include <process.h>
#include <errno.h>
void main(){
int ret = _execl("cmd.exe", "gzip.exe", "C:\\Users\\user_name\\work\\Db618\\test.txt");
printf("ret: %d \t strerror: %s\n", ret, strerror(errno));
}有人能举例说明如何使用这个函数吗?我在寻找解决方案时又发现了一个API system(),但在使用之前,我想知道这两个系统在Windows平台上的区别是什么?
发布于 2020-07-30 09:45:19
根据_execl:您的第一个参数不需要是cmd.exe,但应该是命令行的第一个命令,如gzip.exe。
您可以参考MSDN sample。
最后,您的程序只需要删除初始的"cmd.exe",但需要注意的是,最后一个参数必须为NULL以表示终止。
代码如下:
#include <stdio.h>
#include <process.h>
#include <errno.h>
#include <cstring>
int main(int argc, const char* argv[])
{
int ret = _execl("D:\\gzip-1.3.12-1-bin\\bin\\gzip.exe" ,"-f","D:\\gzip-1.3.12-1-bin\\bin\\test.txt" ,NULL);
printf("ret: %d \t strerror: %s\n", ret, strerror(errno));
return 0;
}如果您想使用system,您可以将命令作为参数传递给system function,就像使用CMD来达到相同的效果一样。
您可以像这样使用它:
system("gzip.exe test.txt");https://stackoverflow.com/questions/63157728
复制相似问题