code 提取文件夹下所有文件的名字(包括子文件夹)
2015-12-22 12:38
239 查看
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<dirent.h>
#include<unistd.h>
#include<vector>
#include<string>
#include<iostream>
using namespace std;
int readFileList(char *basePath)
{
DIR *dir;
struct dirent *ptr;
char base[1000];
vector<string> file_names;
if((dir =opendir(basePath))==NULL)
{
perror("Open direrror");
exit(-1);
}
while((ptr =readdir(dir))!=NULL)
{
if(strcmp(ptr->d_name,".")==0|| strcmp(ptr->d_name,"..")==0)
continue;
//file
else if(ptr->d_type == 8)
{
file_names.push_back(ptr->d_name);
//printf("d_name:%s%s\n",basePath,ptr->d_name);
}
//link file
else if(ptr->d_type ==10)
printf("d_name:%s%s\n",basePath,ptr->d_name);
//dir
else if(ptr->d_type == 4)
{
memset(base,'\0',sizeof(base));
strcpy(base,basePath);
strcat(base,"/");
strcat(base,ptr->d_name);
readFileList(base);
}
}
for(vector<string>::iterator it= file_names.begin();it!=file_names.end();it++)
{
cout<<basePath<<"/"<<*it<<endl;
}
closedir(dir);
return 0;
}
int main()
{
DIR *dir;
char basePath[1000];
memset(basePath,'\0',sizeof(basePath));
getcwd(basePath,999);
printf("the current diris:%s\n",basePath);
strcat(basePath,"/123");
readFileList(basePath);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<dirent.h>
#include<unistd.h>
#include<vector>
#include<string>
#include<iostream>
using namespace std;
int readFileList(char *basePath)
{
DIR *dir;
struct dirent *ptr;
char base[1000];
vector<string> file_names;
if((dir =opendir(basePath))==NULL)
{
perror("Open direrror");
exit(-1);
}
while((ptr =readdir(dir))!=NULL)
{
if(strcmp(ptr->d_name,".")==0|| strcmp(ptr->d_name,"..")==0)
continue;
//file
else if(ptr->d_type == 8)
{
file_names.push_back(ptr->d_name);
//printf("d_name:%s%s\n",basePath,ptr->d_name);
}
//link file
else if(ptr->d_type ==10)
printf("d_name:%s%s\n",basePath,ptr->d_name);
//dir
else if(ptr->d_type == 4)
{
memset(base,'\0',sizeof(base));
strcpy(base,basePath);
strcat(base,"/");
strcat(base,ptr->d_name);
readFileList(base);
}
}
for(vector<string>::iterator it= file_names.begin();it!=file_names.end();it++)
{
cout<<basePath<<"/"<<*it<<endl;
}
closedir(dir);
return 0;
}
int main()
{
DIR *dir;
char basePath[1000];
memset(basePath,'\0',sizeof(basePath));
getcwd(basePath,999);
printf("the current diris:%s\n",basePath);
strcat(basePath,"/123");
readFileList(basePath);
return 0;
}
相关文章推荐
- Arduino 控制8 channel 5V继电器模组
- python 10min系列之实现增删改查系统
- 买房小计
- c++ --> extern "C" {}详解
- Cache学习笔记汇总
- iOS 项目的目录结构能看出你的开发经验
- 咏南开发框架之分类
- Apache日志分析代码大全
- Eclipse SVN安装
- iOS开发UI篇—UIScrollView控件实现图片缩放功能
- iOS获取当前app版本号
- 手机赚钱学生赚软件必备网赚工具
- webView用法小结
- 短信验证的实现(基于云之讯短信验证SDK)
- 咏南开发框架之报表
- Libgdx: 将Texturepacker打包的PNG图片还原成一张一张的单个的
- iOS 学会使用delegate和block<一>
- Mac环境下svn的使用
- iOS去掉html 的转义符号
- 咏南开发框架之单据处理