为什么文件不是由 f stream 创建的,或者即使它的输出只是垃圾值?

why is the file not being created by f stream or even if it is the output is just garbage values?

我必须使用 Turbo C++ 设计一个基本的医院管理系统。一切似乎都工作正常,包括添加记录功能,但是当我选择查看输入的记录(通过使用同一程序创建的主菜单)时,该功能才开始显示随机字符的无限循环。此外,我似乎找不到在 C:\TurboC++\Disk\TurboC3\BIN.

中创建的文件

我认为可能出现问题是因为文件未创建,并且可能存在另一个与 class.

同名的文件

p.s。我知道我正在使用一个古老的遗物,所以你不需要提及它,而且这是我的第一个学期,所以我仍然不了解所有技术知识,所以如果你尽可能保持语言简单以提供帮助,我将不胜感激我和其他像我一样的人。

class hospital {
  int bill,
  ref,
  age;
  char disease[30],
  name[20],
  doctor[20],
  address[50],
  sex;
  public : void getdata() {
    cout<<"ENTER THE NAME OF THE PATIENT"<<endl;
    gets(name);
    cout<<endl<<"ENTER THE ADDRESS OF THE PATIENT"<<endl;
    gets(address);
    cout<<endl<<"ENTER THE NAME OF THE DIESEASE(S) OF THE PATIENT"<<endl;
    gets(disease);
    cout<<endl<<"ENTER THE SEX OF THE PATIENT(f/m)"<<endl;
    cin>>sex;
    cout<<endl<<"ENTER THE NAME OF THE DOCTOR"<<endl;
    gets(doctor);
    cout<<endl<<"ENTER THE PRESCRIPTION/REFERENCE no."<<endl;
    cin>>ref;
    cout<<endl<<"ENTER THE AGE OF THE PATIENT"<<endl;
    cin>>age;
    cout<<endl<<"ENTER THE BILL OF THE PATIENT"<<endl;
    cin>>bill;
  }
  void display() {
    cout<<endl<<"THE DETAILS OF THE PATIENT ARE AS FOLLOWS:"<<endl;
    cout<<"AGE:"<<age<<"\tREFERENCE/PRESCRIPTION no.:"<<ref;
    cout<<"\tTHE NAME OF PATIENT:";
    puts(name);
    cout<<endl<<"\tTHE NAME OF THE DOCTOR:";
    puts(doctor);
    cout<<endl<<"\tTHE DIAGNOSED/TESTED FOR DISEASE(S) IS/ARE :"<<endl;
    puts(disease);
    cout<<endl<<"THE SEX OF THE PATIENT IS "<<sex<<endl;
    cout<<endl<<"THE BILL OF THE PATIENT IS "<<bill<<endl;
    cout<<endl<<"THE ADDRESS OF THE PATIENT IS: "<<endl;
    puts(address);
  }
  int retref() {
    return ref;
  }
}

;
void main() {
  clrscr();
  hospital h;
  cout<<endl<<"\n\n\t\t\tLOADING...\n\t\tPLEASE WAIT..\n";
  system("pause");
  clrscr();
  cout<<endl<<"\tWELCOME TO THE HOSPITAL MANAGEMENT SYSTEM"<<endl;
  displayrec();
  getch();
}

这是查看所有已保存记录的功能

void displayrec()
{
  hospital h;
  ifstream fin;
  fin.open("hospitalrecords.dat", ios: :binary);
  while(!fin.eof()) {
    h.display();
    if(fin.eof()) break;
  }
}

您没有从文件中读取任何内容,因此eof()始终为假,从而导致无限循环。

你需要在循环中实际读取一些东西,并用你读取的数据初始化h