linux读取目录内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5734802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading directory content in linux
提问by Sushant Jain
How can we read the contents (subdirectories and filenames) of a directory using C language in Linux.
我们如何在 Linux 中使用 C 语言读取目录的内容(子目录和文件名)。
回答by dmuir
Relevant functions are opendir, readdir and closedir. I recommend "man opendir" etc
相关函数有 opendir、readdir 和 closedir。我推荐“man opendir”等
回答by Stewart Dale
Here is code that will do this (taken from link), hopefully it will get you started on what you are looking for.
这是执行此操作的代码(取自链接),希望它能让您开始寻找所需的内容。
#include <ftw.h>
#include <stdio.h>
#include <sys/stat.h>
int list(const char *name, const struct stat *status, int type);
int main(int argc, char *argv[]) {
if(argc == 1)
ftw(".", list, 1);
else
ftw(argv[1], list, 1);
return 0;
}
// FTW_F The object is a file
// FTW_D ,, ,, ,, ,, directory
// FTW_DNR ,, ,, ,, ,, directory that could not be read
// FTW_SL ,, ,, ,, ,, symbolic link
// FTW_NS The object is NOT a symbolic link and is one for
// which stat() could not be executed
int list(const char *name, const struct stat *status, int type) {
if(type == FTW_NS)
return 0;
if(type == FTW_F)
printf("0%3o\t%s\n", status->st_mode&0777, name);
if(type == FTW_D && strcmp(".", name) != 0)
printf("0%3o\t%s/\n", status->st_mode&0777, name);
return 0;
}
Edit: ftw is what is used to walk the filesystem, for more info about it please refer to its man page ftw man page
编辑:ftw 用于遍历文件系统,有关它的更多信息,请参阅其手册页ftw 手册页
回答by Terminal
Here is a recursive program to print the name of all subdirectories and files recursively.
Usage: ./a.out path name
Error conditions are not checked for initial path name supplied as command line argument.
Basic flow of code:
All the entries in current directory are read.
if it is directory name, its name is added to path name and and function is called recursively.
else name of the files are printed.
这是一个递归程序,用于递归打印所有子目录和文件的名称。
用法:./a.out 路径名
不检查作为命令行参数提供的初始路径名的错误条件。
代码基本流程:
读取当前目录中的所有条目。
如果是目录名,则将其名称添加到路径名并递归调用函数。
否则打印文件名。
Details about the particular functions can be referenced in respective man pages as pointed by dmuir:
有关特定功能的详细信息可以在 dmuir 指出的相应手册页中引用:
#include<sys/stat.h>
#include<unistd.h>
#include<dirent.h>
#include<error.h>
int read(char *pth)
{
char path[1000];
strcpy(path,pth);
DIR *dp;
struct dirent *files;
/*structure for storing inode numbers and files in dir
struct dirent
{
ino_t d_ino;
char d_name[NAME_MAX+1]
}
*/
if((dp=opendir(path))==NULL)
perror("dir\n");
char newp[1000];
struct stat buf;
while((files=readdir(dp))!=NULL)
{
if(!strcmp(files->d_name,".") || !strcmp(files->d_name,".."))
continue;
strcpy(newp,path);
strcat(newp,"/");
strcat(newp,files->d_name);
printf("%s\n",newp);
//stat function return a structure of information about the file
if(stat(newp,&buf)==-1)
perror("stat");
if(S_ISDIR(buf.st_mode))// if directory, then add a "/" to current path
{
strcat(path,"/");
strcat(path,files->d_name);
read(path);
strcpy(path,pth);
}
}
}
int main(int argc,char *argv[])
{
read(argv[1]);
}