int main(){
int choice;
fstream in, out;
switch(choice)
{
case 1:
filename = s.studentRegister();
out.open(filename.c_str(), ios::out);
out.write((char *)&s, sizeof(Student));
cout<<endl<<"Registered successfully."<<endl;
case 2:
s.studentLogin();
}
}
class Student
{
std::string studentName, roll, studentPassword;
fstream in, out;
public:
std::string studentRegister()
{
cout<<"Enter roll number"<<endl;
cin>>roll;
cout<<"Enter current semester"<<endl;
cin>>ch;
cout<<"Enter your name"<<endl;
cin>>studentName;
cout<<"Enter password"<<endl;
cin>>studentPassword;
return (roll+".dat");
}
void studentLogin()
{
Student s;
cout<<"Enter roll number: "<<endl;
cin>>roll;
cout<<"Enter password: "<<endl;
cin>>studentPassword;
filename = roll + ".dat";
in.open(filename.c_str(), ios::in);
in.read((char *)&s, sizeof(Student));
read.close();
if((studentPassword.compare(s.studentPassword)==0))
{
system("cls");
cout<<"Welcome "<<s.studentName<<"!"<<endl;
displayStudentMenu();
}
else
{
cout<<"Invalid user";
exit(0);
}
}
我的学生班有两个功能:studentRegister()
和studentLogin()
. 调用 studentRegister 时,它接受学生的所有详细信息,然后将相应班级的对象写入 DAT 文件。现在,登录时,我正在尝试将文件的内容读入对象“s”,使用 in.read((char *)&s, sizeof(Student));
但是,这会导致运行时错误并且控制台突然关闭。出了什么问题?