【C++】保存和读取有规律格式的文件路径

我需要对文件样本进行训练,有10*50=500个样本,所以训练之前写个存入和读取程序,方便操作。文件格式规律如下:

charSamples\0\0 (1).png

charSamples\0\0 (2).png

*

*

*

charSamples\9\9 (50).png

#include <vector>
#include <iostream>
#include <fstream>
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/imgproc/imgproc.hpp"  
#include "opencv2/core/core.hpp"
#include <opencv\ml.h>
#include <string>
using namespace cv;
using namespace std;


vector<string> readfiles(string FilePath)
{
	char buffer[256];	
	vector<string> bufstr;
	fstream out;
	out.open(FilePath,ios::in);
	cout<<"com.txt"<<" 的内容如下:"<<endl;
	while(!out.eof())
	{
	   out.getline(buffer,256,'\n');
	   bufstr.push_back(buffer);
	   cout<<buffer<<endl;
	}
	out.close();
	return bufstr;
}

int write2txt(string savename)
{
	char buf[50];
	vector<string> savevec;
	ofstream save(savename,ios::out);
	int counts = 0;
	for(int a =0;a<10;a++)
	{
		for(int j =1;j<51;j++)
		{
			sprintf(buf,"charSamples\\%d\\%d (%d).png\n",a,a,j);					save<<buf;
			savevec.push_back(buf);
			counts+=1;
		}
	}
	save.close();
	return counts;
}

int main()
{
	string savename = "4-11.txt";
	int n = write2txt(savename);
	readfiles(savename);
	cout<<"successful write files num:"<<n<<endl;
	waitKey();
	
	return 0;
}

___________________________________________________________________________________



输出保存的TXT文件:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值