1

我想在 C/C++ 程序中执行这个命令:stat -c "%F %A %n" *filename goes here* 文件名存储在main函数的argv[1].

我试过了execl("/bin/stat", "stat", "-c", "\"%F %A %n\"", "file", NULL);

该命令应该如何execl()实现结果?

4

2 回答 2

2

您的命令应如下所示:

int res = execl("/bin/stat", "stat", "-c", "\"%F %A %n\"", argv[1], NULL);
if (res == -1) {
   perror("/bin/stat");
   exit(1);
}

然后 perror 会告诉你:

/bin/stat: No such file or directory

你会意识到 stat 在 /usr/bin 中,或者使用 execlp 是个好主意。

于 2019-10-10T14:37:30.990 回答
0

我发现了我的问题。该stat命令位于/usr/bin而不是/bin.

于 2019-10-10T15:32:54.100 回答