【发布时间】:2015-03-16 02:31:31
【问题描述】:
所以这个程序应该打印一个目录下的所有文件信息。以下是我的 C++ 代码:
#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <sys/types.h>
using namespace std;
int main(int argc, char *argv[]){
execvp("/bin/ls",&argv[0]);
fork(); // should I put it here?
perror("failed to execute the command");
cout<< "The PID is " << getpid() <<endl;
return 0;
}
我认为该命令应该是ls -a,但如何在程序中实现它?目前它只打印所有文件,而不是每个文件的信息。
另外,我如何打印 PID?因为getpid() 函数似乎不起作用。
谢谢!
【问题讨论】:
标签: c++ linux command-line