Linux下面用c语言遍历目录opendir -> readdir -> closedir
来源:互联网 发布:请各位知悉还是悉知 编辑:程序博客网 时间:2024/06/06 06:47
相关函数是
opendir -> readdir -> closedir
可以分别通过man opendir , man readdir ,man closedir 在linux下面去查询函数用法等.
seekdir,telldir,scandir
linux shell 遍历目录同样经常使用。
一次opendir -> readdir ,只能遍历当前目录的子目录,不会递归遍历。
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *dirname);
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
当然dirend的结构可以在man readdir中找到
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported
by all file system types */
char d_name[256]; /* filename */
};
所以根据这些可以得到代码如下,运行格式为 ./xx <path> //path 为目录路径
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#define MAX_LEN 512
static char* fullpath;
static int dopath();
int main(int argc,char* argv[]){
printf("--------------------------------------------------\n");
fullpath=(char *)malloc(MAX_LEN);
strcpy(fullpath,argv[1]);
printf("fullpath is %s\n",fullpath);
dopath();
int ret;
return 0;
}
static int dopath(){
struct stat statbuf;
DIR *dir;
struct dirent *dirp;
if(lstat(fullpath,&statbuf)<0){
//if get statbuf failed
printf("maybe the fullpath error !!");
return -1;
}
//has got statbuf
printf("S_ISDIR(statbuf.st_mode) is %d\n",S_ISDIR(statbuf.st_mode));
//when S_ISDIR()==1 it is a dir when ==0 maybe it is a file
if(S_ISDIR(statbuf.st_mode)==0){
printf("it is not a dir ,maybe it's a file !!");
return 0;
}
dir = opendir(fullpath);
if( dir ==NULL){
printf("can not read dir\n");
return 0;
}
dirp = readdir(dir);
while(dirp){
dirp = readdir(dir);
printf("inode: %d d_reclen: %d name: %s\n",dirp->d_ino,dirp-
>d_reclen,dirp->d_name);
}
closedir(dir);
return 0;
}
- Linux下面用c语言遍历目录opendir -> readdir -> closedir
- Linux下面用c语言遍历目录opendir -> readdir -> closedir
- linux C 遍历目录及其子目录 opendir -> readdir -> closedir
- linux C 遍历目录及其子目录 opendir -> readdir -> closedir
- linux C 遍历目录及其子目录 opendir -> readdir -> closedir .
- linux C 遍历目录及其子目录 opendir -> readdir -> closedir
- linux C 遍历目录及其子目录 opendir -> readdir -> closedir http://blog.csdn.net/angle_birds/article/details/
- Linux C/C++ ----目录文件的操作(opendir,readdir,closedir)
- linux c下目录文件的操作(opendir,readdir,closedir)
- Linux目录操作函数:opendir/readdir/closedir
- opendir 、closedir 、readdir 目录函数
- Linux chmod opendir readdir closedir
- linux c学习笔记----目录文件的操作(opendir,readdir,closedir)
- linux 目录操作函数opendir readdir closedir,文件stat
- 获取目录信息opendir/readdir/closedir
- linux opendir readdir closedir 的使用
- linux opendir readdir closedir 的使用
- linux opendir 和 readdir , closedir用法
- 等比例缩放图片
- Android.mk详解
- mongo update
- 学会思考程序
- android手机之间通过Wifi传递图片示例--Wi-Fi Peer-to-Peer
- Linux下面用c语言遍历目录opendir -> readdir -> closedir
- XML解析
- Activity Context
- 双11猫狗再度“开撕” 百分之99的人却被它感动哭了
- 如何通过编程挑战成为一个更好的开发者
- 原创SpringMvc+Mybatis+Redis框架
- Android 设计模式之单例实现方式
- Android根据全局变量对activity和fragment的刷新
- 详解 #ifdef #ifndef #undef #else #endif