///[1]文件夹名 [2]后缀格式 [3]保存文件名的文件
///dataTxt txt flieName.txt
///实现同一文件夹下文件名字的获取,并将文件名字保存在txt文件中,BUT 出现了一个问题,第一行不是文件名
#include <io.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void getAllFiles(string filePath, vector<string>& saveFileName, string postfix)
{
struct _finddata_t fileInfo;
long handle = 0;
string pathAndFileName;
if ((handle = _findfirst(pathAndFileName.assign(filePath).append("\\*").c_str(), &fileInfo)) == -1)
{
std::cerr << "没有找到匹配的项目\n" << std::endl;
}
else
{
while (_findnext(handle, &fileInfo) == 0)
{
///string fileDir = fileInfo.name;
saveFileName.push_back(pathAndFileName.assign(filePath).append("/").append(fileInfo.name));
}
}
_findclose(handle);
}
int main(int argc,char** argv)
{
string basedir = argv[1]; ///基本目录
string postfix = argv[2]; ///后缀名||文件扩展名(filename extension)
vector<string> files;
getAllFiles(basedir, files, postfix);
string savingName = argv[3];
ofstream fout(savingName);
//fout << files.size() << endl;
for (size_t i = 0; i < files.size(); i++) ///for (size_t i = 1; i < files.size(); i++) 此法亦可剔除第一行
{
fout << files[i] << endl;
}
fout.close();
std::cerr << "成功读取 " << files.size() << " 个文件" << std::endl;
system("pause");
return 0;
}
《C++》读取同一文件夹下的全部文件名,并写入某一文件中(一)
最新推荐文章于 2023-06-30 17:09:34 发布