c++文件流操作,如何向文件写入对象
- C/C++ code
#include<iostream>#include<stdlib.h>#include<ctime>#include<string>#include<fstream>using namespace std;class FoodCard{private: string Name; float Money; int state; //表示账户是否被冻结 //int OrderCode; //序号public: string Num; //身份证用作标识,设为公有似乎更合理 FoodCard *Next; FoodCard(int times/*int OrderCode*/);//建立新卡时被调用 void Spend(int Money); //如果Money为负,则为充值 void show(); void set(); void change(); void init();};class List{private: FoodCard *foodcard; //头结点 int Size; int maxOCode;public: List(); void insert(); FoodCard* find(); void save(); void destory(); void Delete(FoodCard *foodcard); };/*实现部分*/FoodCard::FoodCard(int times/*int OrderCode*/) { if(times==0) { this->Next=NULL; return; } std::cout<<"请输入姓名(输入“*”以办理临时卡)"<<endl; std::cin>>this->Name; if(this->Name=="*") { this->Num="*"; goto tempcard; } std::cout<<"请输入身份证号码"<<endl; std::cin>>this->Num; //身份证tempcard: std::cout<<"请输入金额(若输入0,则设置为默认金额20元"<<endl; std::cin>>this->Money; if(this->Money==0) this->Money=20; //this->OrderCode=OrderCode; Next=NULL; state=0; //状态为非挂失状态 std::cout<<"饭卡生成成功!"<<endl;}void FoodCard::Spend(int Money){ if(state==0) this->Money=this->Money-Money; else std::cout<<"该卡已经被冻结,无法消费"<<endl;}List::List(){ Size=0; maxOCode=0; foodcard=new FoodCard(0); //为什么要这样才不会异常? }void List::insert(){ FoodCard *p,*q; this->Size++; this->maxOCode++; p=foodcard; //本菜单中的“foodcard” while(p->Next!=NULL) { p=p->Next; } q=new FoodCard(1/*maxOCode*/);//生成新饭卡 p->Next=q; //q->Next=NULL; 这一句和下一句是否有区别 q->Next=p->Next; save();}void FoodCard::init(){ FoodCard::Money=NULL; FoodCard::Name=""; FoodCard::Num=""; FoodCard::state=0;}void FoodCard::set(){ if(state==0) { state=1; cout<<"挂失成功"<<endl; } else { state=0; cout<<"解除挂失"<<endl; }}/*void FoodCard::change(FoodCard *foodcard){ cout<<"选择操作:1.修改姓名;2.修改金额;3.按序号查询;4.退出"<<endl; //cin>>choice; //switch(choice) //{ //case 1: // { // string name; // cout<<"请输入姓名!"<<endl; // cin>> name;};*/FoodCard* List::find(){ string num; FoodCard *p; p=foodcard->Next; std::cout<<"请输入身份证号码"<<endl; std::cin>>num; while(p->Next!=NULL) { if(p->Num==num) return p; p=p->Next; } return foodcard;}void FoodCard::show(){ cout<<Name<<endl; cout<<Num<<endl; cout<<Money<<endl; if(state==1) cout<<"此卡已经挂失"<<endl;}void List::save(){ std::ofstream listfile("FoodCard.info",ios::out); //删除原链表信息,将新的链表信息插入,这似乎是最简单的方法。但是如果信息量大,这种操作无疑是缺乏效率的 if(listfile.fail()) { cout<<"原文件不存在,已经开始创建文件..."<<endl; } listfile.write( ); cout<<"数据已保存"<<endl;}void main(){ //std::time_t now=std::time(0); //cout<<now<<endl; List list; std::ifstream listfile("FoodCard.info",ios::in); std::cout<<"1.添加新饭卡 2.注销旧饭卡 3.设置与解除挂失 4.买饭 5.续钱 6.退出系统"<<endl;Option: cout<<"请输入选项编号:"<<endl; int choice; cin>>choice; switch(choice) { case 1: { list.insert(); }break; case 2: { }break; case 3: { }break; case 4: { }break; case 5: { }break; case 6: { }break; default: cout<<"你的选项非法!"<<endl; goto Option; } system("pause");}
大家请帮忙看看,save函数中的ofstream类对象的成员函数write()的参数列表该如何填写,以前很少用过文件流,见笑了。
[解决办法]
请参考:
一个在加拿大上学的朋友的暑假作业 里面有向文件写对象的代码。
[解决办法]
深入浅出MFC里有描述了怎么序列化对象的
[解决办法]
C++没有内置的序列化支持,写入对象是需要序列化的,而且无法跨平台跨语言,换了个编译器或运行库版本可能都出问题
[解决办法]
- C/C++ code
ifndef COURSE_H#define COURSE_H#include <iostream>#include<string>using namespace std;class Course{public: string CName; string teacher; string homework; double grade; Course(string cn="Course",string tea="Teacher") { CName=cn; teacher=tea; homework=""; grade=0; } void setName(string na) { CName=na; } void setTea(string tea) { teacher=tea; } void setHomework(string hw) { homework=hw; } void setGrade(double grade1,double grade2,double scale) { grade=grade1*scale/100+grade2*(100-scale)/100; } string getName() { return CName; } string getTeacher() { return teacher; } string getHomework() { return homework; } double getGrade() { return grade; }};#endif#ifndef TEACHER_H#define TEACHER_H#include<iostream>#include<string>using namespace std;class Teacher{private: int ID; string Name; string code; Course CN; double scale;public: Teacher(string na="Teacher",int id=0,string co="54321",string cn="Course") { CN.setTea(na); ID=id; Name=na; code=co; CN.setName(cn); CN.setTea(na); scale=0; } void setName(string na){Name=na;} void setID(int id){ID=id;} void setCode(string co){code=co;} void setCourse(string cn){CN.setName(cn);} void setScale(double s) { scale=s; } int getID(){return ID;} double getScale(){return scale;} string getCode(){return code;} string getName(){return Name;} string getCourseName(){return CN.getName();}};#endif#include<iostream>#include<string>#include<fstream>#include<iomanip>#include<cstdlib>using namespace std;#include"Course.h"#include"Teacher.h"void initial(ofstream&);void test(fstream&);int main(){ fstream inT("d:\\teacher.dat",ios::in); if(!inT) { cerr<<"文件不存在!\n-------->新建文件\n"; ofstream outT("d:\\teacher.dat",ios::out); initial(outT); outT.close(); inT.open("d:\\teacher.dat",ios::in); } test(inT); inT.close(); return 0;}void initial(ofstream& outT){ Teacher tea; for(int i=0;i<=2000;i++) { tea.setID(i); outT.seekp(i*sizeof(Teacher)); outT.write(reinterpret_cast<const char*>(&tea),sizeof(Teacher)); cout<<tea.getID()<<" "; }}void test(fstream& io){ Teacher t; for(int i=27;i<=2000;i++) { io.seekg(i*sizeof(Teacher)); io.read(reinterpret_cast<char *>(&t),sizeof(Teacher)); cout<<t.getID()<<endl; }}
[解决办法]
对象的序列化
[解决办法]
什么结构写,什么结构读,类和结构体一样读写就行了
[解决办法]
[解决办法]
重载>>与<<。类输出/输入一个指定的数据结构到文件中