stringstream實例

stringstream的具體做用稍後來總結,這裏分享一個實例,從txt文檔中讀取數據,並對進行處理。ios

#include <iostream>
#include <sstream>  //stringstream的頭文件
#include <fstream>
#include <vector>
#include <string>
using namespace std;

void main()
{
	ifstream fin("d:\\desk\\face detection\\dataset\\train\\trainImageList.txt");
	ofstream fout("d:\\desk\\face detection\\dataset\\train\\trainImageList1.txt");
	char p_data[1024] = { 0 };
	string filename;
	while (fin.getline(p_data, sizeof(p_data)))
	{
		vector<double> data;
		//stringstream resizeData;
		//resizeData.str(p_data);
		stringstream resizeData(p_data);  //依照空格將數據分段
		resizeData >> filename;  //向filename流中寫入值
		while (resizeData)      //數據流輸完後就結束
		{
			string a;
			resizeData >> a;
			data.push_back(atof(a.c_str()));
		}
		
		//data[0] 不變
		double temp = data[1];
		data[1] = data[2];
		data[2] = data[3];
		data[3] = temp;

		//使其爲一個正方形
		double w, h;
		double differenceValue = 0.0;
		w = data[2] - data[0];
		h = data[3] - data[1];
		differenceValue = abs(w - h);
		if (w > h)
			data[3] += differenceValue;
		else
			data[2] += differenceValue;

		fout << filename << " ";
		vector<double>::iterator it = data.begin();
		for (; it != data.end()-1; it++)
		{
			fout << *it << " ";
		}
		fout << endl;
	}
}
相關文章
相關標籤/搜索