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

#include
#include
#include
#include
#include


using namespace std;


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


struct DATA{
vector 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 {
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 &vct, string strFileName)
{
ofstream fout(strFileName.c_str());
if (fout.bad())
{
cout << "error!" << endl;
return;
}


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


fout.close();
return;
}


void fnReadFile(vector &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 vctMyData;
vctMyData.clear();


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


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


return 0;
}

你可能感兴趣的:(C++ 读、写、整理大数据到新文件)