前言
一个小练习,知识点还挺多的,主要功能就是读取一个文件夹下的特定格式文件,并把完整目录写入一个txt文件,程序虽小,知识点不少
.
代码
/*
作者:山科_xxin;
时间:2017年3月22日22:46:51;
功能:读取目录下文件,并把完整目录写入txt;
类别:<a href="https://www.ixxin.cn/tag/c%e5%ad%97%e7%ac%a6%e4%b8%b2/" title="查看更多关于C++字符串的文章" target="_blank">C++字符串</a>练习;
*/
#include<fstream>
#include<iostream>
#include<stdlib.h>
#include<io.h>
#include<vector>
#include<string>
using namespace std;
vector<string> GetFileName(const char *Path)
{
vector<string>files;
long handle = 0;
struct _finddata_t fileinfo;
handle = _findfirst(Path,&fileinfo);
string stmp = Path;
size_t a = stmp.find_last_of('\\');
if (!string::npos)
{
cout<<"查找失败";
}
string t = "\\";
string strName = stmp.substr(0,a)+t;
if (handle==-1)
{
cout<<"false";
exit(0);
}
files.push_back(strName+fileinfo.name);
while(!_findnext(handle,&fileinfo))
{
files.push_back(strName+fileinfo.name);
}
_findclose(handle);
return files;
}
int SaveFname(vector<string>files,const char *Path)
{
ofstream SFname(Path);
int len = files.size();
for (int i =0;i<len;i++)
{
cout<<files[i]<<endl;
SFname<<files[i]<<endl;
}
SFname.close();
return 0;
}
int main()
{
char *Path;
Path = "G:\\opencv\\*jpg";
vector<string> files;
files = GetFileName(Path);
SaveFname(files,"G:\\opencv\\test.txt");
system("pause");
return 0;
}
main函数:

结果:

后语
省略。



