1

Setting up a database which takes the input from a user(First name, last name, ID number , etc..) and inputs the information into a binary file, to update information , delete records, and update any information provided. All other functions seem to be functioning properly(Dont mind the bad code, im fairly new to programming) besides the function to display the non empty records. the information seems to be getting saved into the files because the other functions have access to it(all the same file pointer). I've checked to see if the file was being overwritten by the initialization of the empty file and that hasn't been the case to my understanding.

I've checked to see if the file was being overwritten by the initialization of the empty file and that hasn't been the case to my understanding. I've also tried updating the the if statement in the DisplayRecord function so that it would read all files and display only the ones that have an established ID number but it still shows null when the function is ran.

   // fopen opens the file; exits if file cannot be opened
   if ((cPtr = fopen("patient.dat", "rb")) == NULL) {
  puts("File could not be opened.");
   }//end If
   else {

 printf( "Patient ID\t Last Name\t first name\t DOB \tGender\t Doctor ID\t 
 Doctor last name\t Room Number\n" );

 struct PatientData {
 unsigned int Pat_ID; //ID number
 char F_Name[25];//first name
 char L_Name[25]; //last name
 char Phone_num[20] ; //Phone number
 char gender[2];
 unsigned int doctor_ID;
 char doc_LN[25];
 unsigned int room_num;
 char DoB[10];
};

 //read all records until eof
 while(!feof(cPtr)){

 //create blank set to compare to data on file
  struct PatientData Patient= { 0, "","","","",0,"", 0,"" };

  int result=fread(&Patient,sizeof(struct PatientData), 1 , cPtr);

  if(result!=0 && Patient.Pat_ID!=0){
     printf("%-d%-15s%-15s%-10s%-12s%-15d%-15s%-10d\n",
     Patient.Pat_ID, 
     Patient.L_Name,Patient.F_Name,Patient.DoB,Patient.gender,
     Patient.doctor_ID, Patient.doc_LN,Patient.room_num);
  }
  fclose(cPtr); // fclose closes the file

Its suppose to display all records that have information in it. but the actual output is not showing any records.

KevMo
  • 11
  • 2
  • 3
    Is this C or C++? Looks like C. [Edit] your question to have the correct language tag, and add the definition of the `PatientData` structure. – 1201ProgramAlarm Mar 30 '19 at 18:36
  • 4
    Handy reading: [Why is “while (!feof(file))” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – user4581301 Mar 30 '19 at 18:42
  • Please post a [mcve] so we can reproduce the problem so we can help you debug it. – user3629249 Mar 31 '19 at 16:10
  • regarding: `if(result!=0 && ` the call to `read()` can return a negative value. (which would result in that expression evaluating to `true`. Which is not what you want. Suggest: `if( result > 0 && ` – user3629249 Mar 31 '19 at 16:15
  • OT: regarding: `struct PatientData Patient= { 0, "","","","",0,"", 0,"" };` the initialization of the struct instance: `Patient` is not necessary because the following call to `read()` will completely overlay it – user3629249 Mar 31 '19 at 16:17
  • It is highly probable that the cause of the problem is NOT in the posted code. Please post a [mcve] so we can see how the file contents are generated. – user3629249 Mar 31 '19 at 16:19
  • OT: regarding: `puts("File could not be opened.");` Error messages should be output to `stderr`, not `stdout` and when the error is from a C library function should also output (to `stderr`) the text reason the system thinks the error occurred. Suggest calling: `perror()` as that outputs to `stderr`, both of the desired data – user3629249 Mar 31 '19 at 16:22

0 Answers0