LINUX下历遍目录的方法一般是这样的
打开目录-》读取-》关闭目录
相关函数是
opendir -> readdir -> closedir
#include <dirent.h>
DIR *opendir(const char *dirname);
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
#include <dirent.h>
int closedir(DIR *dirp);
好了,三个函数都出来了呵呵,原型就是上面,给自己解释一下免得以后我自己忘了呵呵!我以经忘过好几回了,所以这次放上博客来
opendir用于打开目录,是类似于流的那种方式,返回一个指向DIR结构体的指针他的参数*dirname是一个字符数组或者字符串常量,
readdir函数用于读取目录,他只有一个参数,这个参数主opendir返回的结构体指针,或者叫句柄更容易理解些吧。这个函数也返回一个结构体指针 dirent *
dirent的结构如下定义
struct dirent
{
long d_ino; /* inode number */
off_t d_off; /* offset to this dirent */
unsigned short d_reclen; /* length of this d_name */
char d_name [NAME_MAX+1]; /* file name (null-terminated) */
}
结构体中d_ino存放的是该文件的结点数目,什么是结点数目呢我也说不清楚了呵呵,查一下其它资料了(这里应该不是节点数目,而是其下文件的索引节点号inode)
d_off 是文件在目录中的编移,具体是什么意思我也不是很明白,我很少用到它,其本上就是用到d_name ,short d_reclen是这个文件的长度,需要注意的是这里的长度并不是指文件大小,因为大小和长度是2回事了,你可以用lseek将文件长度移得很长,但大小其实还是那么大.最后一个元素就是我们要的了,文件名称!
写了一个实例:
/**
* 功能: Linux下C语言目录历遍 (读取目录)
* 作者: 小徐
* 邮箱: xjtdy888@163.com
* QQ: 339534039
* 转载请注明出处
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
void print_usage(void);
void print_usage(void)
{
printf("Usage: test dirname\n");
}
int main(int argc,char *argv[])
{
DIR * dp;
struct dirent *filename;
if (argc < 2)
{
print_usage();
exit(1);
}
dp = opendir(argv[1]);
if (!dp)
{
fprintf(stderr,"open directory error\n");
return 0;
}
while (filename=readdir(dp))
{
printf("filename:%-10s\td_info:%ld\t d_reclen:%us\n",
filename->d_name,filename->d_ino,filename->d_reclen);
}
closedir(dp);
return 0;
}
将上面代码保存为readdir.c执行下面的命令进行编译测试
[root@phpos ~]# gcc readdir.c
[root@phpos ~]# ./a.out
Usage: test dirname
[root@phpos ~]# ./a.out /etc/samba/
先用链表保存文件信息,然后输出:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
int main(int argc,char *argv[])
{
DIR *directory_pointer;
struct dirent *entry;
struct FileList
{
char filename[64];
struct FileList *next;
}start,*node;
char dir[80];
puts( "Please input the directory: ");
gets(dir);
if ((directory_pointer=opendir(dir))==NULL)
printf( "Error opening %s\n ",dir);
else
{
start.next=NULL;
node=&start;
while ((entry=readdir(directory_pointer))!=NULL)
{
node-> next=(struct FileList *)malloc(sizeof(struct FileList));
node=node-> next;
strcpy(node-> filename,entry-> d_name);
node-> next=NULL;
}
closedir(directory_pointer);
node=start.next;
while(node)
{
printf( "%s\n ",node-> filename);
node=node-> next;
}
}
system( "PAUSE ");
return 0;
}
类似dos命令下的tree:
#include "iostream"
#include "string"
#include "iomanip"
#include "io.h"
using namespace std;
void list_dir(const char file_path[], int level = 0) {
char path[128];
strncpy(path, file_path, sizeof(path));
if (level == 0) {
cout<<path<<endl;
} else {
char* name = strrchr(path, '\\');
if (name && *++name) {
for (int i = 0; i < level - 1; ++i) {
cout<<"│ ";
}
cout<<"├─"<<name<<endl;
}
}
strcat(path, "\\*");
_finddata_t fileinfo;
intptr_t handle = _findfirst(path, &fileinfo);
if (handle == -1) {
return;
}
bool bfind = true;
while (bfind) {
if (strcmp(fileinfo.name, "..") &&
strcmp(fileinfo.name, ".") &&
(fileinfo.attrib == 16 || fileinfo.attrib == 17)) {
strncpy(path, file_path, sizeof(path));
strcat(path, "\\");
strcat(path, fileinfo.name);
list_dir(path, level + 1);
}
bfind = _findnext(handle, &fileinfo) == 0 ? true : false;
}
_findclose(handle);
}
int main() {
char path[128];
cout<<"please enter the path : ";
cin>>path;
list_dir(path);
system("pause");
return 0;
}