#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
class student
{
public:
static void get_st();
static void show_st();
static void trunc_st();
private:
string name;
};
void student::trunc_st()
{
ofstream os("student.DAT",ios::trunc);
os.close();
}
void student::get_st()
{
cin.clear();
student s;
cin>> s.name ;
ofstream os("student.DAT",ios::binary|ios::out|ios::app);
os.write(reinterpret_cast<char*>(&s), sizeof(s) );
os.close();
}
void student::show_st()
{
student s;
ifstream infile;
infile.open("student.DAT", ios::in | ios::binary|ios::ate);
infile.seekg(0);
infile.read( reinterpret_cast<char*>(&s), sizeof(s));
while(!infile.eof())
{
cout<<setw(11)<<s.name<<endl;
infile.read( reinterpret_cast<char*>(&s), sizeof(s) );
}
infile.close();
}
int main()
{
char c='n';
student::trunc_st();
do
{
cout<<"Enter the students'datas!"<<endl;
student::get_st();
cout<<"Continue? y/n\n";
cin>>c;
}while(c=='y');
cout<<setw(11)<<"Name :"<<endl;
student::show_st();
return 0;
}
运行后name输出的好象只是几个空格啊,哪位大哥告诉我怎么回事啊!
[此贴子已经被作者于2006-10-2 10:15:27编辑过]