使用linux c 目录操作函数,读取任一目录下的所有目录信息文件信息
2016-06-07 00:07
1146 查看
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <unistd.h>
# include <dirent.h>
# include <sys/types.h>
int re_readdir(char name[])
{
char str[1000] = {0};
//把目录路径存放在字符数组内
strcpy(str, name);
printf("%s\n", str);
//打开目录
DIR *dir = opendir(str);
if(NULL == dir)
{
perror("opendir");
return -1;
}
//如果打开成功,则循环读取目录
struct dirent *ent = readdir(dir);
while(NULL != ent)
{
strcpy(str,name);
printf("name = %s: type = %d\n", ent->d_name, ent->d_type);
if(4 == ent->d_type && strcmp(ent->d_name, ".") && strcmp(ent->d_name, ".."))
{
re_readdir(strcat(strcat(str,ent->d_name), "/"));
}
ent = readdir(dir);
}
closedir(dir);
return 0;
}
int main(void)
{
re_readdir("/home/tarena/uc/");
return 0;
}
# include <string.h>
# include <stdlib.h>
# include <unistd.h>
# include <dirent.h>
# include <sys/types.h>
int re_readdir(char name[])
{
char str[1000] = {0};
//把目录路径存放在字符数组内
strcpy(str, name);
printf("%s\n", str);
//打开目录
DIR *dir = opendir(str);
if(NULL == dir)
{
perror("opendir");
return -1;
}
//如果打开成功,则循环读取目录
struct dirent *ent = readdir(dir);
while(NULL != ent)
{
strcpy(str,name);
printf("name = %s: type = %d\n", ent->d_name, ent->d_type);
if(4 == ent->d_type && strcmp(ent->d_name, ".") && strcmp(ent->d_name, ".."))
{
re_readdir(strcat(strcat(str,ent->d_name), "/"));
}
ent = readdir(dir);
}
closedir(dir);
return 0;
}
int main(void)
{
re_readdir("/home/tarena/uc/");
return 0;
}
相关文章推荐
- centos7安装gitlab
- linux升级mysql到5.7
- Linux 环境Mysql配置记录(持续记录)
- centos No module named yum
- linux主目录下各个子目录的作用
- linux查看硬件信息及驱动设备相关整理
- SELinux入门
- Linux运维有绝招
- linux find grep组合使用
- Linux查找文件内容(grep)
- linux内核移植详解1——板级初始化过程
- Linux系统的进程通常有以下几种状态:
- linux内核移植详解1——板级初始化过程
- CentOS增加网卡
- Linux常见面试题2
- Linux常见面试题1
- ubuntu linux 安装composer
- 【转】linux configure报错configure: error: C++ preprocessor “/lib/cpp” fails sanity 的解决办法
- 装完CentOS-6.x之后的简单优化
- Linux计划任务(转载)