Linux C++ 访问子目录以及里面的文件
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h> using namespace std; #define dmax(a,b) (((a) > (b)) ? (a) : (b))
#define dmin(a,b) (((a) < (b)) ? (a) : (b)) //获取特定格式的文件名
int readFileList(std::vector<string> &filelist, const char *basePath, string format)
{
DIR *dir;
struct dirent *ptr;
char base[]; if ((dir=opendir(basePath)) == NULL)
{
perror("Open dir error...");
exit();
} while ((ptr=readdir(dir)) != NULL)
{
if(strcmp(ptr->d_name,".")== || strcmp(ptr->d_name,"..")==) ///current dir OR parrent dir
continue;
else if(ptr->d_type == ) //file
{
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
string temp = ptr->d_name;
//cout << temp << endl;
string sub = temp.substr(temp.length() - , temp.length()-);
//cout << sub << endl;
if(sub == format)
{
string path = basePath;
path += "/";
path += ptr->d_name;
filelist.push_back(path);
}
}
else if(ptr->d_type == ) ///link file
{
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
}
else if(ptr->d_type == ) ///dir
{
memset(base,'\0',sizeof(base));
strcpy(base,basePath);
strcat(base,"/");
strcat(base,ptr->d_name);
readFileList(filelist, base, format);
}
}
closedir(dir);
return ;
} //找出目录中所有子目录
int findAllSubDir(std::vector<string> &filelist, const char *basePath)
{
DIR *dir;
struct dirent *ptr;
char base[]; if ((dir=opendir(basePath)) == NULL)
{
perror("Open dir error...");
exit();
} while ((ptr=readdir(dir)) != NULL)
{
if(strcmp(ptr->d_name,".")== || strcmp(ptr->d_name,"..")==) ///current dir OR parrent dir
continue;
else if(ptr->d_type == ) //file
{
// //printf("d_name:%s/%s\n",basePath,ptr->d_name);
// string temp = ptr->d_name;
// //cout << temp << endl;
// string sub = temp.substr(temp.length() - 4, temp.length()-1);
// //cout << sub << endl;
// if(sub == format)
// {
// string path = basePath;
// path += "/";
// path += ptr->d_name;
// filelist.push_back(path);
// }
}
else if(ptr->d_type == ) ///link file
{
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
}
else if(ptr->d_type == ) ///dir
{
memset(base,'\0',sizeof(base));
strcpy(base,basePath);
strcat(base,"/");
strcat(base,ptr->d_name);
filelist.push_back(ptr->d_name);
findAllSubDir(filelist, base);
}
}
closedir(dir);
return ;
} void findDir(string src, string &facefolder, string &facenameindex, string filePath)
{
int begin = src.find(filePath) + filePath.size() + ;
int end = ;
for (int i = src.size() - ; i >= ; --i)
{
//cout << src[i] << endl;
if (src[i] == '/')
{
end = i;
break;
}
}
//cout << begin << endl;
//cout << end << endl;
facefolder = src.substr(begin, end - - begin + );
facenameindex = src.substr(end + , src.size() - - (end + ) + );
} void GetStringFileName(const string &filePath, string &filename, string &fileformat)
{
int fileformat_begin = ;
int fileformat_end = filePath.length() - ;
int filename_begin = ;
int filename_end = filePath.length() - ;
for (int i = filePath.length() - ; i >= ; --i)
{
//cout << filePath[i] << endl;
if (filePath[i] == '.')
{
fileformat_begin = i + ;
filename_end = i - ;
} if (filePath[i] == '/')
{
filename_begin = i + ;
break;
}
}
// cout << filename_begin << endl;
// cout << filename_end << endl;
filename = filePath.substr(filename_begin, filename_end - filename_begin + );
fileformat = filePath.substr(fileformat_begin, fileformat_end - fileformat_begin + );
} void StringSplit(const string &src, const char splitchar, vector<string> &dst)
{
int begin = ;
int end = ;
int i = ;
for (i = ; i < src.length(); ++i)
{
if(src[i] == splitchar)
{
end = i - ;
dst.push_back(src.substr(begin, end - begin + ));
begin = i + ;
end = begin;
}
} //last
if(i > end)
{
end = i - ;
}
dst.push_back(src.substr(begin, end - begin + ));
} //遍历一个目录,找出其中某一后缀的所有文件
void sence0()
{
// Loop over all the images provided on the command line.
std::vector<string> srcfiles;
string srcpath = "./src/1";
string srcformat = ".txt";
string outputformat = ".png";
printf("the current dir is : %s\n", srcpath.c_str());
readFileList(srcfiles, srcpath.c_str(), srcformat);
string dstpath = "./src/3";
printf("the dst dir is : %s\n", dstpath.c_str());
if (access(dstpath.c_str(), ) == -)
{
int flag=mkdir(dstpath.c_str(), );
} std::vector<string> dstfiles(srcfiles.size());
for (int i = ; i < srcfiles.size(); ++i)
{
/* code */
cout << srcfiles[i] << endl;
string filename;
string fileformat;
GetStringFileName(srcfiles[i], filename, fileformat);
string dstfile = dstpath + "/" + filename + "." + fileformat;
dstfiles[i] = dstfile;
cout << dstfiles[i] << endl;
}
} //建立多级目录,包括子目录, 并依次处理文件,适用于小文件
void sence1()
{
// Loop over all the images provided on the command line.
std::vector<string> files;
string filePath = "./lfw_small_raw";
string format = ".jpg";
string outputformat = ".png";
printf("the current dir is : %s\n", filePath.c_str());
readFileList(files, filePath.c_str(), format);
string dstpath = "./lfw_small_convert";
printf("the dst dir is : %s\n", dstpath.c_str());
if (access(dstpath.c_str(), ) == -)
{
int flag=mkdir(dstpath.c_str(), );
} std::vector<string> alignimg(files.size());
for (int i = ; i < files.size(); ++i)
{
/* code */
cout << files[i] << endl;
string facefolder;
string facenameindex;
findDir(files[i], facefolder, facenameindex, filePath);
facenameindex = facenameindex.substr(, facenameindex.size() - );
facenameindex += outputformat;
//cout << facefolder << endl;
//cout << facenameindex << endl;
string newfacefolder = dstpath + "/" + facefolder;
if (access(newfacefolder.c_str(), ) == -)
{
int flag=mkdir(newfacefolder.c_str(), );
}
alignimg[i] = newfacefolder + "/" + facenameindex;
cout << alignimg[i] << endl;
}
} //依次遍历文件夹中的每一个目录,遇到一个目录新建一个目录,然后遍历该目录的文件
void sence2()
{
// Loop over all the images provided on the command line.
std::vector<string> sudDirfiles;
string srcpath = "./lfw_small_raw";
string dstpath = "./lfw_small_convert"; //如果目录存在就删除目录
if (access(dstpath.c_str(), ) == )
{
cout << "remove " << dstpath << endl;
int flag = rmdir(dstpath.c_str());
} //如果目录不存在就新建
if (access(dstpath.c_str(), ) == -)
{ cout << "mkdir " << dstpath << endl;
int flag=mkdir(dstpath.c_str(), );
} findAllSubDir(sudDirfiles, srcpath.c_str());
for (int i = ; i < sudDirfiles.size(); ++i)
{
//cout << sudDirfiles[i] << endl;
//遍历当前子目录中所有文件
std::vector<string> srcfiles;
string srcSudDir = srcpath + "/" + sudDirfiles[i];
string srcformat = ".jpg";
printf("the current subdir is : %s\n", srcSudDir.c_str()); string dstSudDir = dstpath + "/" + sudDirfiles[i];
//建立目标子目录
if (access(dstSudDir.c_str(), ) == -)
{
cout << "mkdir " << dstSudDir << endl;
int flag=mkdir(dstSudDir.c_str(), );
}
printf("the current subdir is : %s\n", dstSudDir.c_str()); readFileList(srcfiles, srcSudDir.c_str(), srcformat);
for (int j = ; j < srcfiles.size(); ++j)
{
cout << srcfiles[j] << endl;
string filename;
string fileformat;
GetStringFileName(srcfiles[j], filename, fileformat);
string dstfile = dstSudDir + "/" + filename + "." + fileformat;
cout << dstfile << endl;
}
}
} int main()
{
sence2();
// string filePath = "./lfw_small_raw/Aaron_Eckhart";
// std::vector<string> split;
// StringSplit(filePath, '/', split);
// string curSubDir = split[split.size() - 1];
// cout << curSubDir << endl;
// for (int i = 0; i < split.size(); ++i)
// {
// cout << split[i] << endl;
// } // string filename;
// string fileformat;
// GetStringFileName(filePath, filename, fileformat);
// //cout << filePath.substr(2, 3) << endl;
// cout << filePath << endl;
// cout << filename << endl;
// cout << fileformat << endl;
return ;
}
Linux C++ 访问子目录以及里面的文件的更多相关文章
- Linux 终端访问 FTP 及 上传下载 文件
今天同事问我一个问题,在Linux 下访问FTP,并将文件上传上去. 我之前一直是用WinSCP工具的. 先将文件从linux copy到windows下,然后在传到ftp上.google 一下. 方 ...
- Linux怎样访问Windows共享文件和文件夹
常常使用Windows的人可能会发现,Windows计算机之前共享资料非常方便,但是有时候想玩玩Linux的时候,如Fedora.Ubuntu.CentOS等,该怎样才能访问Windows计算机上的文 ...
- Linux 终端访问 FTP 及 上传下载 文件[转]
1. Linux 终端连接FTP [oracle@Dave ~]$ ftp 10.85.7.97 Connected to 10.85.7.97. 220 Serv-U FTP Server ...
- linux下删除目录及其子目录下某种类型文件
Linux下,如果想要删除目录及其子目录下某种类型文件,比如说所有的txt文件,则可以使用下面的命令: find . -name "*.txt" -type f -print -e ...
- win下gvim或者linux下的vim安装vundle都适用的配置文件 - 在当前目录及其子目录下**, 的所有文件* 中, 搜索当前光标所在的单词
gvim下的普通配置: if v:lang =~ "utf8$" || v:lang =~ "UTF-8$" set fileencodings=utf-8,g ...
- Linux查看当前目录下所有子目录是否包含某个文件
在Linux下,当需要找某个文件但又不知道这个文件在哪个具体的目录下,这时可以使用全目录查找 使用find命令: find . -type f -name "job_21_output*&q ...
- linux上搭建nginx+ftp,实现文件的上传与访问
ftp服务器搭建 1.新建用户ftpuser并指定主目录为/home/ftpuser (注意:这个目录是后面存储和读取文件的目录) <!--创建用户并指定主目录--> useradd -d ...
- Linux centos7 VMware Apache访问日志不记录静态文件、访问日志切割、静态元素过期时间
一.Apache访问日志不记录静态文件 网站大多元素为静态文件,如图片.css.js等,这些元素可以不用记录 vim /usr/local/apache2.4/conf/extra/httpd-vho ...
- linux复制指定目录下的全部文件到另一个目录中
linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...
随机推荐
- ubuntu中gitlab搭建
1.gitlab介绍 gitlab是一款代码仓库管理工具,可以用来搭建自己的代码管理服务器. gitlab包自带了redis,nginx,postgresql,unicorn等众多服务组件. 2.硬件 ...
- javascript基础拾遗(四)
1.什么是闭包 正常函数,执行完毕后相关的参数,变量就释放掉了. 当一个函数的返回值是另一个函数时,该函数的相关参数和变量都会保存在返回的函数中,这种结构叫做闭包. 2.示例 计算数组和 functi ...
- 每日英语:Redfin Real-Estate Firm Gets Cold Shoulder in Silicon Valley
"I used to think I was this made man," says entrepreneur Glenn Kelman. "That's what t ...
- 每日英语:Marriage makes our children richer — Here's why
Young people from less-privileged homes are more likely to graduate from college and earn more if ra ...
- 分享几个免费IP地址查询API接口
几个免费IP地址查询API接口 1.IP地址查询接口:http://apis.juhe.cn/ip/ip2addr要先去https://www.juhe.cn/docs/api/...申请APPKEY ...
- AIX上打包排除某些文件/文件夹
Syntax X/Open Standards: tar {-c|-r|-t|-u|-x} [-B] [ -d ] [ -E ] [ -F ] [-h ] [ -i ] [ -l ] [ -m ] [ ...
- 设计模式之装饰模式(iOS开发,代码用Objective-C展示)
在面向对象编程中有个重要的原则,里氏代换原则:一个软件实体如果使用的是一个父类的话,那么一定适用其子类,而且它察觉不出父类对象与子类对象的区别.也就是说,在软件设计里面,把父类替换成它的子类,程序的行 ...
- refiling失败报错Invalid function: org-preserve-local-variables
refiling失败报错Invalid function: org-preserve-local-variables,原因: elc,不太清楚 解决办法: 删除org??目录下的elc文件 https ...
- PHP中的WebService
Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应用之间, 无论它们所使用的语言. ...
- ARKit从入门到精通(8)-ARKit捕捉平地
转载:http://blog.csdn.net/hdfqq188816190/article/details/73360287 1.1-ARKit捕捉平地实现流程介绍 1.2-完整代码 1.3-代码下 ...