?? 程序14.11:使用抽象數(shù)據(jù)類型的文件輸出和輸入.cpp
字號:
/* 程序14.11:使用抽象數(shù)據(jù)類型的文件輸出和輸入.cpp:*/
#include<iostream> //包含頭文件
#include<fstream> //包含頭文件
using namespace std; //使用名字空間std
class student
{
private:
int iReg_no;
char cName[20];
public:
void setRegno() //設(shè)置變量iReg_no
{
cout<<"請輸入學(xué)生的序號 : ";
cin>>iReg_no;
}
void setName() //設(shè)置變量cName
{
cout<<"請輸入學(xué)生的名稱 : ";
cin>>cName;
}
int getRegno() //通過函數(shù)訪問變量iReg_no
{
return iReg_no;
}
char *getName() //通過函數(shù)訪問變量cName
{
return cName;
}
};
int main()
{
ofstream outObj("student.dat");
student stud;
char ch;
while(1)
{
cout<<"\n你想輸入更多記錄嗎(y/n)? ";
cin>>ch;
if(ch=='n'||ch=='N')
break;
stud.setRegno(); //設(shè)置變量iReg_no
stud.setName(); //設(shè)置變量cName
outObj.write((char*)&stud,sizeof(student));
}
outObj.close(); //關(guān)閉文件
cout<<"********輸入結(jié)束********"<<endl;
cout<<"\n你想看文件的內(nèi)容嗎(y/n)? ";
cin>>ch;
if(ch=='y'||ch=='Y')
{
ifstream inObj("student.dat");
cout<<"序號"<<"\t名稱"<<endl;
inObj.read((char*)&stud,sizeof(student));
while(inObj)
{
cout<<stud.getRegno()<<"\t";
cout<<stud.getName()<<endl;
inObj.read((char*)&stud,sizeof(student));
}
}
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -