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 ...
随机推荐
- 【Unity】4.0 第4章 创建基本的游戏场景
分类:Unity.C#.VS2015 创建日期:2016-04-05 一.简介 上一章我们学习了如何利用长方体(Cube)制作基本的3D模型,以及如何导入各种资源,本章将在此基础上,分别制作路面.跳板 ...
- Git 工具 - 凭证存储
凭证存储 如果你使用的是 SSH 方式连接远端,并且设置了一个没有口令的密钥,这样就可以在不输入用户名和密码的情况下安全地传输数据. 然而,这对 HTTP 协议来说是不可能的 —— 每一个连接都是需要 ...
- schema中字段类型的定义
当schema中字段类型为String时,保存的时候如果该字段为Number也可以保存成功,mongoose会自动将其转换为数字字符串. 当schema中字段类型为Number时,保存的时候如果该字段 ...
- 支付宝对账单下载Java沙箱调用
package code; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; impo ...
- __slots__ Python Class限制添加属性
正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: class Student(object): pa ...
- Python利用jieba获取中文词汇等
import jieba import os import jieba.analyse data = cleaned_comments # 数据来源于评论数据 seg = jieba.lcut(dat ...
- H3C AP实现定时重启
#job radio_diable view system time 1 repeating at 03:00 command wlan radio disable all time 2 rep ...
- 【Turing Award】Robin Milner And Butler W. Lampson
1991 罗宾·米尔纳(Robin Milner) Robin Milner(13 January 1934 – 20 March 2010) Introduction : Milner was bo ...
- 第21章 RTX 低功耗之睡眠模式
低功耗是 MCU 的一项非常重要的指标,比如某些可穿戴的设备,其携带的电量有限,如果整个电路消耗的电量特别大的话,就会经常出现电量不足的情况,影响用户体验. 本章节为大家讲解 M3/4的低功耗方式之睡 ...
- 3. sklearn的K-Means的使用
1. K-Means原理解析 2. K-Means的优化 3. sklearn的K-Means的使用 4. K-Means和K-Means++实现 1. 前言 在机器学习中有几个重要的python学习 ...