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 ...
随机推荐
- 使用JSR-303进行后台数据校验
一.在SringMVC中使用 使用注解 1.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-validator-4.2.0.Fina ...
- Java 编程中关于异常处理的 10 个最佳实践
异常处理是Java 开发中的一个重要部分.它是关乎每个应用的一个非功能性需求,是为了处理任何错误状况,比如资源不可访问,非法输入,空输入等等.Java提供了几个异常处理特性,以try,catch 和 ...
- 每日英语:Patent Wars Erupt Again in Tech Sector
The long-running patent war among the technology industry's heavyweights just grew a whole lot bigge ...
- 笔记本貌似好了(HP 450 卡)
2013年9月份在苏宁上 买了个HP450,配置应该算还勉强,i5, 4G, 照理说一般LOL,DOTA,应该还可以.但是经常在打完一盘后,切出来,卡的要命,一直没有解决,昨天晚上虚拟机切出来,更是, ...
- LeetCode: Pascal's Triangle 解题报告
Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...
- Python3将两个有序数组合并为一个有序数组
[本文出自天外归云的博客园] 第一种思路,把两个数组合为一个数组然后再排序,问题又回归到冒泡和快排了,没有用到两个数组的有序性.(不好) 第二种思路,循环比较两个有序数组头位元素的大小,并把头元素放到 ...
- java结合js获取验证码
框架springmvc 1.后台java代码: package com.fh.controller.system.secCode; import java.awt.Color; import java ...
- js实现完美身份证号有效性验证(转)
转载自:http://www.cnblogs.com/lzrabbit/archive/2011/10/23/2221643.html 最近需要对身份证合法性进行验证,实名验证是不指望了,不过原来的验 ...
- hystrix服务降级和服务熔断的区别
故事的背景是这样的:由于小强在工作中碰到一些问题,于是想请教一下业界大牛小壮.于是发生了下面的两个场景: 小强在拿起常用手机拨号时发现该手机没有能够拨通,所以就拿出了备用手机拨通了某A的电话,这个过程 ...
- Python进阶(三十五)-Fiddler命令行和HTTP断点调试
Python进阶(三十五)-Fiddler命令行和HTTP断点调试 一. Fiddler内置命令 上一节(使用Fiddler进行抓包分析)中,介绍到,在web session(与我们通常所说的se ...