Linux - 打印文件夹全部文件 代码(C)

列出文件夹全部文件 代码(C)


本文地址:http://blog.csdn.net/caroline_wendy


首先配置环境,參考:http://blog.csdn.net/caroline_wendy/article/details/39743345


输出文件夹中的全部文件名称

代码:

/*By C.L.Wang
 * Eclipse CDT
 * Ubuntu 12.04
 * 2014.10.5*/

#include "apue.h"
#include "error.h"
#include <dirent.h>

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)








posted @ 2016-04-03 20:00  zfyouxi  阅读(472)  评论(0编辑  收藏  举报