C++文件I/O 以对象写文件后,读出对象时出现乱码,不知道怎么解决,贴出代码如下.
2009-04-15 16:24
591 查看
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class Student;
istream & operator >> (istream & is,Student& student);
ostream & operator << (ostream & os,Student &student);
bool writeStudent(vector<Student> & vt);
vector<Student> & readStudent(vector<Student> & vt);
void displayStudent(vector<Student> & vt);
class Student{
private:
string name;
string number;
int age;
public:
Student();
Student(string ,string,int);
friend istream & operator >> (istream & is,Student &student);
friend ostream & operator << (ostream & os,Student &student);
friend bool writeStudent(vector<Student> & vt);
friend vector<Student> & readStudent(vector<Student> & vt);
};
Student::Student()
{
this->age=0;
this->name=name;
this->number=number;
}
Student::Student(string name ,string number,int age){
this->age=age;
this->name=name;
this->number=number;
}
//重载>>操作符
istream & operator >> (istream &is,Student &student)
{
cout<<"请输入姓名、学号、年龄"<<endl;
is>>student.name>>student.number>>student.age;
return is;
}
//重载<<操作符
ostream & operator << (ostream & os,Student &student)
{
os<<student.name<<"/t"<<student.number<<"/t"<<student.age;
return os;
}
//添加学生
vector<Student> & addStudent(vector<Student> & vt)
{
cout<<"start adding"<<endl;
int flag=0;
while(flag!=1)
{
Student *m_stu=new Student;
cin>>*m_stu;
vt.push_back(*m_stu);
delete m_stu;
cout<<"继续添加吗,1代表结束添加,0代表继续添加,1 or 0 ?"<<endl;
cin>>flag;
}
return vt;
}
//向文件中写学生信息
bool writeStudent(vector<Student> & vt)
{
ofstream myFile;
myFile.open("info.txt",ios::out|ios::binary);
if(!myFile)
{
cout<<"open txt error"<<endl;
return false;
}
for(int i=0;i<vt.size();i++)
{
cout<<i<<":"<<vt[i].name<<vt[i].number<<endl;
myFile.write((char *)(&vt[i]),sizeof(Student));
}
myFile.close();
return true;
}
//从文件中读信息
vector<Student> & readStudent(vector<Student> & vt)
{
ifstream myFile;
myFile.open("info.txt",ios::in|ios::binary);
if(!myFile)
{
cout<<"open txt error"<<endl;
return vt;
}
while(myFile.peek()!=EOF)
{
cout<<"reading"<<endl;
Student *stu=new Student;
myFile.read((char *)stu,sizeof(Student));
vt.push_back(*stu);
delete stu;
}
myFile.close();
displayStudent(vt);
return vt;
}
//显示学生信息
void displayStudent(vector<Student> & vt)
{
cout<<"姓名"<<"/t"<<"学号"<<"/t"<<"年龄"<<endl;
vector<Student>::iterator it=vt.begin();
while(it!=vt.end())
{
cout<<*it<<endl;
it++;
}
}
void main()
{
vector<Student> stuV;
stuV=readStudent(stuV);
stuV=addStudent(stuV);
displayStudent(stuV);
if(writeStudent(stuV))
cout<<"写入成功"<<endl;
else
cout<<"写入失败"<<endl;
}
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class Student;
istream & operator >> (istream & is,Student& student);
ostream & operator << (ostream & os,Student &student);
bool writeStudent(vector<Student> & vt);
vector<Student> & readStudent(vector<Student> & vt);
void displayStudent(vector<Student> & vt);
class Student{
private:
string name;
string number;
int age;
public:
Student();
Student(string ,string,int);
friend istream & operator >> (istream & is,Student &student);
friend ostream & operator << (ostream & os,Student &student);
friend bool writeStudent(vector<Student> & vt);
friend vector<Student> & readStudent(vector<Student> & vt);
};
Student::Student()
{
this->age=0;
this->name=name;
this->number=number;
}
Student::Student(string name ,string number,int age){
this->age=age;
this->name=name;
this->number=number;
}
//重载>>操作符
istream & operator >> (istream &is,Student &student)
{
cout<<"请输入姓名、学号、年龄"<<endl;
is>>student.name>>student.number>>student.age;
return is;
}
//重载<<操作符
ostream & operator << (ostream & os,Student &student)
{
os<<student.name<<"/t"<<student.number<<"/t"<<student.age;
return os;
}
//添加学生
vector<Student> & addStudent(vector<Student> & vt)
{
cout<<"start adding"<<endl;
int flag=0;
while(flag!=1)
{
Student *m_stu=new Student;
cin>>*m_stu;
vt.push_back(*m_stu);
delete m_stu;
cout<<"继续添加吗,1代表结束添加,0代表继续添加,1 or 0 ?"<<endl;
cin>>flag;
}
return vt;
}
//向文件中写学生信息
bool writeStudent(vector<Student> & vt)
{
ofstream myFile;
myFile.open("info.txt",ios::out|ios::binary);
if(!myFile)
{
cout<<"open txt error"<<endl;
return false;
}
for(int i=0;i<vt.size();i++)
{
cout<<i<<":"<<vt[i].name<<vt[i].number<<endl;
myFile.write((char *)(&vt[i]),sizeof(Student));
}
myFile.close();
return true;
}
//从文件中读信息
vector<Student> & readStudent(vector<Student> & vt)
{
ifstream myFile;
myFile.open("info.txt",ios::in|ios::binary);
if(!myFile)
{
cout<<"open txt error"<<endl;
return vt;
}
while(myFile.peek()!=EOF)
{
cout<<"reading"<<endl;
Student *stu=new Student;
myFile.read((char *)stu,sizeof(Student));
vt.push_back(*stu);
delete stu;
}
myFile.close();
displayStudent(vt);
return vt;
}
//显示学生信息
void displayStudent(vector<Student> & vt)
{
cout<<"姓名"<<"/t"<<"学号"<<"/t"<<"年龄"<<endl;
vector<Student>::iterator it=vt.begin();
while(it!=vt.end())
{
cout<<*it<<endl;
it++;
}
}
void main()
{
vector<Student> stuV;
stuV=readStudent(stuV);
stuV=addStudent(stuV);
displayStudent(stuV);
if(writeStudent(stuV))
cout<<"写入成功"<<endl;
else
cout<<"写入失败"<<endl;
}
相关文章推荐
- 用java代码写xml文件时,出现中文乱码的解决方法
- IO 流读取文件时候出现乱码 文件编码格式问题 怎么转换解决方法
- 项目文件.NET下编译C++代码时出现fatal error C1083: 无法打开包括文件:“xuhk_JNIHelloWorld.h”: No such file or directory错误的解决方法。
- IO 流读取文件时候出现乱码 文件编码格式问题 怎么转换解决方法
- flash加载脚本文件导致IE脚本错误 ,行53 ,字符3,缺少对象,代码0 , 怎么解决?
- .NET下编译C++代码时出现fatal error C1083: 无法打开包含文件:“iostream.h”: No such file or directory错误的解决方法.
- 引用原因.NET下编译C++代码时出现fatal error C1083: 无法打开包含文件:“iostream.h”: No such file or directory错误的解决方法.
- IO 流读取文件时候出现乱码 文件编码格式问题 怎么转换解决方法
- 可运行代码 文本框 插入html code 更新以后出现乱码。怎么解决?
- CSDN博客中C++相关代码出现 <span style="white-space:pre"> </span> 乱码问题解决方法
- .NET下编译C++代码时出现fatal error C1083: 无法打开包括文件:“xuhk_JNIHelloWorld.h”: No such file or directory错误的解决方法。
- C / C++ 读取文件出现乱码解决方法
- Cursor扫描本地音乐文件,英文名也出现乱码,怎么解决。
- mysql数据库导入SQL文件出现乱码,如何解决?(经验总结、实测有效)
- Java读取UTF-8格式txt文件第一行出现乱码——问号“?”及解决;Java读带有BOM的UTF-8文件乱码原因及解决方法
- FileReader读取文件出现乱码的解决办法
- 解决文件下载时文件名出现乱码的问题
- .NET下编译C++代码时出现"error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int"错误的解决方法。
- Intellj idea 上编写上传案例,上传文件时,出现文件名乱码问题描述及解决方式
- myeclipse中java文件中文乱码怎么解决