C++ 读、写、整理大数据到新文件

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>


using namespace std;


const int ID_SIZE = 2;//对处理当前文本库中的ID的位数定义,test.txt中使用5个byte。


struct DATA{
vector<string> vctDataList;
};


struct MyData{
string strID;
DATA data;
};


bool fnIsHeadLine(string str)
{
if (str.substr(0,2)!="0x")
{
return false;
}
else
{
if (str.size()>=2+ID_SIZE*3)//0x29,94,01,83,39,至少这么长;
{
for (int i=1; i<=ID_SIZE; i++)
{
string strIdent = str.substr(2+i*3 - 1,1);
if (strIdent == ",")
{
continue;
}
else
{
return false;
}
}
return true;
}
else
{
return false;
}
}
}


string fnGetIDfromStr(string str)
{
//带0x的返回;
string strResult = "";


//假设源数据:0x29,94,01,78,76,45 ;


string strNew = str.substr(2);//去掉0x;


for (int i=0; i<ID_SIZE; i++)
{
strResult += "0x";

strResult += strNew.substr(i*3,2);


if (i!=(ID_SIZE-1))
{
strResult += ",";
}
}


return strResult;
}


string fnGetFirstLineData(string str)
{
string strResult = "";


//去掉尾部的空格;


string strNew = str.substr(0,str.size()-1);


//找最后一个逗号的位置;


int iPos = strNew.find_last_of(",");
if (iPos!=string::npos)
{
strResult = strNew.substr(iPos+1);//从逗号开始取到结尾;
}


return strResult;
}


void fnWriteFile(vector<MyData> &vct, string strFileName)
{
ofstream fout(strFileName.c_str());
if (fout.bad())
{
cout << "error!" << endl;
return;
}


for (unsigned int i=0; i<vct.size(); i++)
{
fout << vct[i].strID << "\t";
for (unsigned int j=0; j<vct[i].data.vctDataList.size(); j++)
{
fout << "\"" <<vct[i].data.vctDataList[j] << "\"";
if (j != (vct[i].data.vctDataList.size()-1))
{
fout << ",";
}
}
fout << "\n";
}


fout.close();
return;
}


void fnReadFile(vector<MyData> &vct, string strFileName)
{
ifstream fin(strFileName.c_str());
if (fin.bad())
{
cout << "error" << endl;
return;
}


string str = "";
while (getline(fin,str))
{
if (str!="")
{
//判断它是不是作为行首;
if (fnIsHeadLine(str))
{
MyData myData;


myData.strID = fnGetIDfromStr(str);


myData.data.vctDataList.push_back(fnGetFirstLineData(str));


vct.push_back(myData);
}
//不是行首,则在尾部附加;
else
{
if (vct.size()<1)
{
continue;
}
MyData &mydData = vct[vct.size()-1];//获取vct中最后一个ID的内容,在其基础上附加;


mydData.data.vctDataList.push_back(str.substr(0,str.size()-1));//不要最后一个空格;
}
}
}
fin.close();
return;
}


int main(void)
{
//数据存储;
vector<MyData> vctMyData;
vctMyData.clear();


//读;
fnReadFile(vctMyData,"test.txt");


//写;
fnWriteFile(vctMyData,"new_test.txt");


return 0;
}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭