在C++中对文件的操作,常见的有在指定路径中创建文件夹、获取所有文件夹名称、搜索全部文件、搜索特定类型的文件,先对该操作做以总结,便于以后查询使用。
代码如下:
#include<Shlwapi.h>
#include<iostream>
#include<string>
#include<vector>
#include<io.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
//创建文件夹
//输入:
// IDofOrbitPath 包含数字编号的字符串
// begin 截取的数字开始的位置
// end 截取的长度
//输出:
// 无
void CreateDir(string IDofOrbitPath, int begin, int end)
{
//string MainName = IDofOrbitPath.substr(begin, end);
string DataPath = "D:\\";
string a0 = "Sub\\";
string a1 = MainName;
bool flag = CreateDirectory((DataPath + a0).c_str(), NULL);
}
//依据给定路径,获取该路径下所有的文件夹名称
//输入:
// lpPath 指定路径
// path 获取的指定路径下的文件夹名
//返回:
// int
int find_all_file( char * lpPath,vector<string> &files)
{
char szFind[MAX_PATH];
WIN32_FIND_DATA FindFileData;
strcpy(szFind, lpPath);
strcat(szFind, "\\*.*");
HANDLE hFind = ::FindFirstFile(szFind, &FindFileData);
if (INVALID_HANDLE_VALUE == hFind)
return -1;
do
{
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0)
{
//发现子目录,递归之
char szFile[MAX_PATH] = { 0 };
strcpy(szFile, lpPath);
strcat(szFile, "\\");
strcat(szFile, FindFileData.cFileName);
files.push_back(szFile);
}
}
} while (::FindNextFile(hFind, &FindFileData));
::FindClose(hFind);
return 0;
}
//将string字符串依据特定符号进行分割,返回其各个分块
//输入:
// s 待分割的字符串
// v 分割后的部分存储在容器中
// c 依据分割的符号
//返回:
// 无
void SplitString( string s, vector<string>& v, string c)
{
string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while (string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2 - pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.length())
v.push_back(s.substr(pos1));
}
//依据给定路径,获取该路径下所有文件
//输入:
// lpPath 指定路径
// path 获取的指定路径下的文件夹名
//返回:
// int
int find_all_files( char * lpPath,vector<string> &file)
{
char szFind[MAX_PATH];
WIN32_FIND_DATA FindFileData;
strcpy(szFind, lpPath);
strcat(szFind, "\\*.*");
HANDLE hFind = ::FindFirstFile(szFind, &FindFileData);
if (INVALID_HANDLE_VALUE == hFind)
return -1;
do
{
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0)
{
//发现子目录,递归之
char szFile[MAX_PATH] = { 0 };
strcpy(szFile, lpPath);
strcat(szFile, "\\");
strcat(szFile, FindFileData.cFileName);
}
}
else
{
std::string fullPath = lpPath;
fullPath += "\\";
fullPath += FindFileData.cFileName;
file.push_back(fullPath);
}
} while (::FindNextFile(hFind, &FindFileData));
::FindClose(hFind);
return 0;
}
//依据给定路径,获取该路径下指定类型的文件
//输入:
// lpPath 指定路径
// type 需要搜索的指定类型的文件类型
// file 搜索得到的指定类型的文件
//返回:
// 无
void find_all_Designed_files(string lpPath, string type, vector<string> &file)
{
vector<string> tempFile;
int result = find_all_files(const_cast<char*>(lpPath.c_str()), tempFile);
vector<string> tempPart;
for (int i = 0; i < tempFile.size(); i++)
{
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
const char* pc = (tempFile[i]).data();
_splitpath(pc, drive, dir, fname, ext);
if (ext == type)
{
string fname_str = fname;
string ext_str = ext;
string file_str = fname_str + ext_str;
file.push_back(file_str);
}
}
}
//获取指定文件夹下的所有文件夹名称
int main01()
{
//获取该路径下的文件夹名称
vector<string> path;
cout << "请输出文件夹路径路径:" << endl;
string str;
cin >> str;
char* strChar = const_cast<char*>(str.c_str());
int temp = find_all_file(strChar, path);
vector<vector<string>> FileName;
cout << "输出所有文件夹的路径:" << endl;
for (int i = 0; i < path.size(); i++)
{
vector<string> tempFileName;
SplitString(path[i], tempFileName, "\\");
FileName.push_back(tempFileName);
cout << path[i] << endl;
}
cout << endl << "输出文件夹名称:" << endl;
for (int i = 0; i < FileName.size(); i++)
{
int id = FileName[i].size();
cout << FileName[i][id - 1] << endl;
}
system("pause");
return 0;
}
//获取指定文件夹下的所有文件
void main02()
{
//获取该路径下的文件夹名称
vector<string> path;
cout << "请输出文件夹路径路径:" << endl;
string str;
cin >> str;
vector<string> files;
int result = find_all_files(const_cast<char*>(str.c_str()), files);
for (int i = 0; i < files.size(); i++)
{
cout << files[i] << endl;
}
system("pause");
}
void main03()
{
char *path = "K:\\test";
string type = ".txt";
vector<string> file;
find_all_Designed_files(path, type, file);
for (int i = 0; i < file.size(); i++)
{
cout << file[i] << endl;
}
system("pause");
}
其中测试的文件夹所在路径:K:\\test,包含的内容如下:
测试main01()的结果如下:(得到所有的文件夹名称)
测试main02()的结果如下:(获取所有的文件)
测试main03()的结果如下:(获取特定类型的文件)
报错处理:
[1]出现错误:无法将参数从“WCHAR[256]”转换为"const char":
改成未设置即可
声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。