在一次项目中,软件产品已经开发完成,现在要进行需求验证,测试一下需求的功能有没有完全实现,当时的测试用例非常之多,多了恐怖的地步,当然是编写测试脚本完成了,可问题是,这么多的数据不能放在一个txt文档中,主要有两个原因,一,数据之间有分类关系,二,在一个txt文件中不易后期管理统计,三,光是打开这高数据量的txt系统就要响应好久~~
这时候就需要对一个文件夹下的多个文件进行读写操作了,期间我也走了许多弯路,哎,还是调研了半天才出来结果,惭愧呀,基础不好,现在分享出来让大家在做类似操作时不要走那么多弯路了,有现成的参考就好!哎,说起来,可以引用我这blog的程序员们好幸福啊,直接copy一下稍作修改就好~~
话不多了,上传代码:
#include <iostream>
#include <string>
#include <fstream>
#include <io.h>
using namespace std;
//传入文件路径及文件名,按行输出文件中内容
void GetLineAndPrint(string in_name)
{
ifstream fin(in_name);
if(!fin)
{
cerr<<"open file error"<<endl;
exit(-1);
}
string str;
while(getline(fin,str))
{
cout<<str<<endl;
}
}
int main()
{
struct _finddata_t fileinfo;
string in_path;
string in_name;
cin>>in_path;
string curr = in_path+"\\*.txt";
long handle;
if((handle=_findfirst(curr.c_str(),&fileinfo))==-1L)
{
cout<<"没有找到匹配文件!"<<endl;
return 0;
}
else
{
in_name = in_path + "\\" + fileinfo.name ;
GetLineAndPrint(in_name);
while(!(_findnext(handle,&fileinfo)))
{
in_name = in_path + "\\" +fileinfo.name;
GetLineAndPrint(in_name);
}
_findclose(handle);
}
}
注:以上代码在VS2010中编译通过,代码实现:输入路径,将该路径下所有txt文档内容按行输出至命令行
后续有时间就写通过配置文件传递路径参数以及其他参数