列出目录所有文件 代码(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)
时间: 2024-10-19 01:51:36