#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class student {
private:
char* a;
char* b;
char* c;
public:
student();
~student();
void display()const;
void set() const;
};
student::student()
{
a=new char[10];
b=new char[10];
c=new char[10];
cout<<"construct"<<endl;
}
student::~student()
{
delete a;
delete b;
delete c;
cout<<"destroyed"<<endl;
}
void student::display() const{
cout<<setw(10)<<a<<setw(10)<<b<<setw(10)<<c<<endl;
}
void student::set() const
{
cout<<"cin a"<<endl;
cin>>a;
cout<<"cin b"<<endl;
cin>>b;
cout<<"cin c"<<endl;
cin>>c;
}
void main (){
student a[3];
ofstream abc("a.txt",ios::out|ios::ate);
for (int i=0;i<3;i++)
{
abc.write(reinterpret_cast<char*>(&a[i]),sizeof(student));
a[i].set();
}
abc.close();
}
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class student {
private:
char* a;
char* b;
char* c;
public:
student();
~student();
void display()const;
void set() const;
};
student::student()
{
a=new char[10];
b=new char[10];
c=new char[10];
cout<<"construct"<<endl;
}
student::~student()
{
delete a;
delete b;
delete c;
cout<<"destroyed"<<endl;
}
void student::display() const{
cout<<setw(10)<<a<<setw(10)<<b<<setw(10)<<c<<endl;
}
void student::set() const
{
cout<<"cin a"<<endl;
cin>>a;
cout<<"cin b"<<endl;
cin>>b;
cout<<"cin c"<<endl;
cin>>c;
}
void main (){
student a[3];
ifstream bcd("d:\\a.doc",ios::in);
bcd.seekg(ios::beg);
for(int j=0;j<3;j++)
{
bcd.read(reinterpret_cast<char*>(&a[j]),sizeof(student));
a[j].display();
}
bcd.close();
}
当第一个程序建立文件之后,用第二个程序读出文件后都是乱码,是我VC什么东西没有调还是程序又问题?