2
我正在编写一个读取和写入学生使用类的记录的程序。这个程序不打开文件。所以我无法从文件中读取数据。以下代码是文件读/写在单个文件中。但此代码无法创建文件
class Student
{
private:
unsigned roll ;
char name[30];
float perc;
public:
void getvalue()
{
cout<<"enter rollno , name and percentage :\n";
cin>>roll;
cin.ignore();
cin>>name>>perc;
}
void display()
{
cout << "\nRoll No : " << roll << "\nName : " << name
<< endl << "percentage : " << perc << endl;
}
};
int main()
{
char choice;
Student st ;
fstream file1;
file1.open("stud_rec1.bin", ios::binary|ios::in|ios::out);
do
{
cout<<"\n Detail of student :\n";
st.getvalue();
file1.write((char*)(&st) , sizeof(st));
cout<<"\nwant to input more record(y/n) : ";
cin>>choice;
} while(tolower(choice) == 'y');
file1.seekg(0,ios::beg);
while(file1.read((char*)(&st) , sizeof(st)) )
{
cout<<"1";
st.display();
}
file1.close();
getch();
}
请经常检查你文件之前的任何其他文件操作 – TryinHard