#include <iostream>
#include <stdio.h>
#include <map>
#include <string>
using namespace std;
char tt[100];
void GetChar (const char* buf,char* begin,char* end)
{
char* temp = new char[1000];
strcpy(temp,buf);//拷贝给temp
while(*temp != NULL)
{
if(*temp == *begin)
{
char* bt = temp;
while(*bt != NULL)
{
if(*bt == *end)
{
temp++;
temp++;
*bt = '\0';
strcpy(tt,temp);
}
else
{
bt++;
}
}
}
else
{
temp++;
}
}
}
char bbb[1000];
void GetCharT(const char* buf,int value)//value是第几段的{}
{
char* Temp = new char[1000];
strcpy(Temp,buf);
char c[1];
itoa(value,c,10);//把int转为char
while(*Temp != NULL)
{
AA:
if(*Temp == c[0])
{
char* nTemp = Temp;
nTemp ++;
if(*nTemp == 'M')
{
//找到了头
while(nTemp != NULL)
{
if(*nTemp == ')')
{
*nTemp = '\0';
strcpy(bbb,Temp);
return;
}
else
{
nTemp ++;
}
}
}
else
{
Temp++;
goto AA;//不是头,继续找
}
}
else
{
Temp++;
}
}
}
class student
{
public:
student(){}
~student(){}
char* _name;
int _Lv;
float _money;
int _hp;
int _Exp;
};
void main()
{
FILE* pFile = NULL;
pFile = fopen("Test.txt","r");
char buf[100];
memset(buf,0,100);
student zs;
map<string,student*> mapClass;
mapClass["1"] = &zs;
fread(buf,1,100,pFile);
cout<<"读取当前硬盘上的数据是:"<<buf<<endl;
GetChar(buf,"M","/");
// cout<<"名字是:"<<tt<<endl;
zs._name = new char[10];
strcpy(zs._name,tt);
GetChar(buf,"D","/");
// cout<<"等级是:"<<tt<<endl;
zs._Lv = atoi(tt);
GetChar(buf,"J","/");
// cout<<"金钱是:"<<tt<<endl;
zs._money = atoi(tt);
GetChar(buf,"H","/");
// cout<<"HP是:"<<tt<<endl;
zs._hp = atoi(tt);
GetChar(buf,"E","/");
// cout<<"经验是:"<<tt<<endl;
zs._Exp = atoi(tt);
//查找第2个账户
GetCharT(buf,2);//结果保存在bbb里面
student ls;
mapClass["2"] = &ls;
GetChar(bbb,"M","/");
// cout<<"名字是:"<<tt<<endl;
ls._name = new char[10];
strcpy(ls._name,tt);
GetChar(bbb,"D","/");
// cout<<"等级是:"<<tt<<endl;
ls._Lv = atoi(tt);
GetChar(bbb,"J","/");
// cout<<"金钱是:"<<tt<<endl;
ls._money = atoi(tt);
GetChar(bbb,"H","/");
// cout<<"HP是:"<<tt<<endl;
ls._hp = atoi(tt);
GetChar(bbb,"E","/");
// cout<<"经验是:"<<tt<<endl;
ls._Exp = atoi(tt);
cout<<endl;
for(int i = 0; i<mapClass.size();i++)
{
cout<<"当前账户第"<<(i+1)<<"个账户==="<<endl;
int Temp = (i+1);
itoa(Temp,tt,10);
cout<<"名字是"<<mapClass[tt]->_name<<" ";
cout<<"等级是"<<mapClass[tt]->_Lv<<" ";
cout<<"金钱是"<<mapClass[tt]->_money<<" ";
cout<<"HP是"<<mapClass[tt]->_hp<<" ";
cout<<"经验是"<<mapClass[tt]->_Exp<<" ";
cout<<endl;
cout<<endl;
}
cout<<"读取硬盘数据完毕"<<endl;
fclose(pFile);
system("pause");
}
将Test.txt中的内容分类输出
(1M:张三 /D:21 /J:3000 /H:2100 /E:98989898/)(2M:李四 /D:18 /J:1500 /H:1100 /E:2289898/)
用VS2010可以正常的输出;但是当用QT5时,第二个账户输出有误,QT不知道怎么弄的。