列出目录所有文件 代码(C)
本文地址:外链网址已屏蔽
首先配置环境,参考:外链网址已屏蔽
输出目录中的所有文件名
代码:
/*By C.L.Wang
* Eclipse CDT
* Ubuntu 12.04
* 2014.10.5*/
#include "apue.h"
#include "error.h"
#include
int main(int argc, char* argv[]) {
DIR *dp;
struct dirent *dirp;
if (argc != 2)
err_quit("usage: ls directory_name");
if ((dp = opendir(argv[1])) == NULL)
err_sys("can't open %s", argv[1]);
while ((dirp = readdir(dp)) != NULL)
printf("%s\n", dirp->d_name);
return 0;
}
参数:
/dev
输出:
.
..
log
fb0
dvdrw
dvd
cdrw
cdrom
v4l
vcsa6
vcs6
vcsa4
(others)