C++ fstream怎么读取链表?

Lin_Zero 2013-04-21 11:11:15
本人新手,完全菜鸟,望大神们指教 o(∩_∩)o

学生管理系统,想要实现可以保存数据到硬盘









在程序开始的时候读数据。。。。





结尾的时候保存数据。。。。


第一次执行的时候,可以执行

第二次开启,就出现




菜鸟不太懂C++,求指教 感激不尽
...全文
323 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lin_Zero 2013-04-23
  • 打赏
  • 举报
回复
知道错在哪里啦,多谢各位的指导,第一次发帖就有人帮忙,真心感动 !!!!!!!!!!!!!!
引用 6 楼 zhoujielunzhimi 的回复:
1,结构体中不要使用string等不确定长度的字段 2,不要使用ifstream去读取,因为ifstream会把最后一个 结构体 多读一次。
多谢啦,改掉了STRING 类型的数据后,就可以了,感谢哈哈
朝耕暮耘 2013-04-23
  • 打赏
  • 举报
回复
指针很容易出错的...我觉得错误很可能是while(NULL != head_temp->next )
Lin_Zero 2013-04-22
  • 打赏
  • 举报
回复
引用 1 楼 Mage_King 的回复:
while(NULL != head_temp->next )

head中没数据的 只是一个指针。


不懂唉o(︶︿︶)o

在程序开始的时候,我有
Mage_King 2013-04-22
  • 打赏
  • 举报
回复
while(NULL != head_temp->next ) head中没数据的 只是一个指针。
aj3423 2013-04-22
  • 打赏
  • 举报
回复
1. Student类的next需要在构造函数置0,否则运行时候指不定等于什么,会引起崩溃 2. 把对象保存到文件,或者通过网络传输这种问题是序列化问题(serialization),有很多通用的序列化库,比如boost::serialization,可以google c++ 序列化。 如果只是简单的保存,参考这个
#include <iostream>
#include <string>
#include <fstream>

using namespace std;


#pragma once


// save基本类型 int,double, float,bool...
template <class Stream, class T>
void serialize(Stream& stream, T& x) {
	stream.write((const char*)&x, sizeof(T));
}
// load
template <class Stream, class T>
void de_serialize(Stream& stream, T& x) {
	stream.read((char*)&x, sizeof(T));
}

// 指针
template <class Stream, class T>
void serialize(Stream& stream, T* x) {
	bool is_not_null_ptr = x != 0;
	serialize(stream, is_not_null_ptr);
	if(x) {// if x != 0
		serialize(stream, *x);
	}
}
template <class Stream, class T>
void de_serialize(Stream& stream, T*& x) {
	bool is_not_null_ptr;
	de_serialize(stream, is_not_null_ptr);
	if(is_not_null_ptr) {
		x = new T;
		de_serialize(stream, *x);
	}
}

// std::string
template <class Stream>
void serialize(Stream& stream, const std::string& str) {
	int len = str.length();
	serialize(stream, len);
	stream.write(str.c_str(), len);
}
template <class Stream>
void serialize(Stream& stream, std::string& str) {
	serialize(stream, (const std::string&)str);
}

// string read
template <class Stream>
void de_serialize(Stream& stream, std::string& str) {
	int len;
	de_serialize(stream, len);
	str.resize(len);
	char x;
	for(int i=0; i<len; i++) {
		de_serialize(stream, x);
		str[i] = x;
	}
}


// 以上部分就是个简单的序列化库了


struct Student {
	string num;
	string name;
	double score;
	Student* next;

	Student() : next(0) {} // next置0
	Student(const string& num, const string& name, double score)
		: num(num), name(name), score(score), next(0)
	{}

	void debug() {
		cout << "name: " << name << ", num: " << num 
			<< ", score: " << score << endl;
		if(next) {
			next->debug();
		}
	}
};

template<typename Stream>
void serialize(Stream& s, Student& stu) {
	serialize(s, stu.num); //保存Student的各属性
	serialize(s, stu.name);
	serialize(s, stu.score);
	serialize(s, stu.next);
}
template<typename Stream>
void de_serialize(Stream& s, Student& stu) {
	de_serialize(s, stu.num);
	de_serialize(s, stu.name);
	de_serialize(s, stu.score);
	de_serialize(s, stu.next);
}

int main() {
	Student s1("111", "s1", 100);
	Student s2("222", "s2", 60);
	s2.next = &s1; // s2包含s1

	ofstream ofs("save.txt", ios::binary);
	serialize(ofs, s2);
	ofs.close();

	Student s3;
	ifstream ifs("save.txt", ios::binary);
	de_serialize(ifs, s3);

	s3.debug();//就是上面s2的内容

}

sumos 2013-04-22
  • 打赏
  • 举报
回复
1,结构体中不要使用string等不确定长度的字段 2,不要使用ifstream去读取,因为ifstream会把最后一个 结构体 多读一次。
lm_whales 2013-04-22
  • 打赏
  • 举报
回复
没有你这样读写数据的,string 不是简单类型 要设计好数据格式再读写
赵4老师 2013-04-22
  • 打赏
  • 举报
回复
代码功能不是被人看出来的;而是被单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中分析出来的。
赵4老师 2013-04-22
  • 打赏
  • 举报
回复
单步调试和设断点调试是程序员必须掌握的技能之一。

64,643

社区成员

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

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