C++ 对象持久化????

ruanmengkai 2010-01-05 10:52:17
如何把存在txt中的数据读取出来,并把数据作为对象的成员值呢????
比如class CClass
{
public:
int point;
float passRate;
float firstScore;
float entranceScore;
int teacherNo;
int classNo;
CClass();
virtual ~CClass();

};
在txt中的数据
班号 教师号 高考 摸底考 通过率 积点分
1 1 101.3 74.4 0.9318 307
2 2 111.8 77.2 1 306
3 3 108.9 75.3 0.9 300
4 4 109.2 71 0.875 291
5 3 107.8 72 0.9512 291
6 1 97.9 72.7 0.8157 274
7 5 109.3 73.7 0.9318 284
8 6 108.9 72.3 0.8684 262
9 7 106.8 69.3 0.8 253
10 8 101.8 67.1 0.7 236
...全文
340 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruanmengkai 2010-01-07
  • 打赏
  • 举报
回复
问题已自己解决,非常感谢大家的帮助,尤其是6楼的兄弟
附上代码
string line;
fin.open("my.txt");
while(getline(fin,line))
{
istringstream istr(line);
CClass cs;
istr>>cs.classNo>>cs.teacherNo>>cs.entranceScore>>cs.firstScore>>cs.passRate>>cs.point;
classes.push_back(cs);
}
fin.close();
ruanmengkai 2010-01-06
  • 打赏
  • 举报
回复
我这么写行吗???
char buf[256];
fin.open("my.txt");
fin.getline(buf,256);
istringstream istr(buf);
for(vector<CClass>::iterator iter=classes.begin();iter!=classes.end();iter++)
istr>>*iter;
fin.close();
操作符>>我重载过了
istringstream & operator >>(istringstream &istr,CClass &cs)
{
istr>>cs.classNo>>cs.entranceScore>>cs.firstScore>>cs.passRate>>cs.point>>cs.teacherNo;
return istr;
}
怎么编译通不过啊
错误error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
执行 cl.exe 时出错.
macrojj 2010-01-06
  • 打赏
  • 举报
回复

void readData()
{
ifstream infile("123.txt");
CClass info[10];
for(int i=0; i<10; i++)
{
infile>>info[i].classNo>>info[i].teacherNo>>info[i].firstScore>>info[i].entranceScore
>>info[i].passRate;
}

}
sj13426074890 2010-01-06
  • 打赏
  • 举报
回复
char buf[256];
ifstream ifile("data.txt");
ifile.getline(buf,256);
istringstream istr(buf);
CClass cc;
istr>>cc.classNo>>cc.teacherNo>>cc....


或者
ifstream ifile("data.txt");
CClass cc;
ifile>>cc.classNo>>cc.teacherNo>>cc....
ruanmengkai 2010-01-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lvp1984 的回复:]
方法很多,最简单的是用ifstream读一行,在分割字段
[/Quote]
这个大哥,能不能具体点啊,我急着赶课设。。。谢谢
lvp1984 2010-01-06
  • 打赏
  • 举报
回复
方法很多,最简单的是用ifstream读一行,在分割字段
ruanmengkai 2010-01-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hello_shadow 的回复:]
class CClass 
{
public:
int point;
float passRate;
float firstScore;
float entranceScore;
int teacherNo;
int classNo;
CClass();
virtual ~CClass();
friend istream &operator>>(istream &is,CClass &cs);
};
istream &operator>>(istream &is,CClass &cs)
{
    is>>cs.classNo>>cs.teacherNo................;
}
ifstream ifile("data.txt");
CClass cls();
ifile>>cls;
[/Quote]
那怎么读取下面几行的对象啊???要把每一行一个对象,读取出来
江东橘子 2010-01-06
  • 打赏
  • 举报
回复
自己设置txt的格式 按照格式读取文件就好了
damingg 2010-01-06
  • 打赏
  • 举报
回复


注意数据类型,应该是这样:
istr>>cs.classNo>>cs.teacherNo>>cs.entranceScore>>cs.firstScore>>cs.passRate>>cs.point;

班号 教师号 高考 摸底考 通过率 积点分
1 1 101.3 74.4 0.9318 307

你的是
istr>>cs.classNo>>cs.entranceScore>>cs.firstScore>>cs.passRate>>cs.point>>cs.teacherNo;
0.9318为float,而cs.point为int
梦之安魂曲 2010-01-05
  • 打赏
  • 举报
回复
class CClass
{
public:
int point;
float passRate;
float firstScore;
float entranceScore;
int teacherNo;
int classNo;
CClass();
virtual ~CClass();
friend istream &operator>>(istream &is,CClass &cs);
};
istream &operator>>(istream &is,CClass &cs)
{
is>>cs.classNo>>cs.teacherNo................;
}
ifstream ifile("data.txt");
CClass cls();
ifile>>cls;
clhposs 2010-01-05
  • 打赏
  • 举报
回复
很明显 你的数据是以空白符间隔
直接用C++ 标准文件流读取就行了

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧