If it got this far it would return to the calling function, because you have a stray semicolon at the end, so the lines within the braces below would automically be executed.Code:> if (fread( &patient, sizeof( struct pat_record ), 1, fPtr ) !=1);
If it got this far it would return to the calling function, because you have a stray semicolon at the end, so the lines within the braces below would automically be executed.Code:> if (fread( &patient, sizeof( struct pat_record ), 1, fPtr ) !=1);
It's probably crashing somewhere in the calling function, because any code below the fread is never executed.
Ok... so here goes what I meant
[edit]The indentation was killing me, plus I optimized your printf()'s a bit.[/edit]Code:void binupdaterecord(FILE *fPtr){ int pat_no, pat_max char transaction[20]; struct pat_record patient = {0}; if(!fPtr) { fprintf(stderr, "Invalid file handle.\n"); return; } fseek(fPtr, 0, SEEK_SET); pat_max = fseek(fPtr, 0, SEEK_END); if(pat_max == 0) { fprintf(stderr, "The database is empty!\n"); return; } pat_max /= sizeof(struct pat_record); do { printf("Enter the number of the patient to be updated\n:"); scanf("%d",&pat_no); if(pat_no < 1 || pat_no > pat_max) printf("Invalid patient number, please try again.\n"); else break; } while(1); fseek( fPtr,( pat_no - 1 ) * sizeof( struct pat_record ), SEEK_SET ); fread( &patient, sizeof( struct pat_record ), 1, fPtr ); if ( patient.pat_no == 0 ) printf( "Patient number %d has no information.\n", pat_no ); else { printf( "%d%s%d%s\n\nEnter change:", patient.pat_no,patient.name,patient.dob,patient.doctor); scanf("%s",transaction); strncpy(patient.doctor, transaction, sizeof(patient.doctor)); printf( "%d%s%d%s\n\n", patient.pat_no,patient.name,patient.dob,patient.doctor); fseek( fPtr, ( pat_no -1 ) * sizeof( struct pat_record ), SEEK_SET ); fwrite( &patient, sizeof( struct pat_record ), 1, fPtr ); } }
Last edited by master5001; 04-23-2008 at 05:58 PM.
better yet check the return values of functions that can fail like fread, fwrite, etc
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I agree 100%, vart, which is why I actually do check the return value of fseek(), and just make the good faith assertion that if it is possible to read the file enough to determine its size, then a viable read can be done. In this instance though, its painfully clear that his file handle is invalid.
Hi again, sorry was away for a while, master5001, thanks for the code, with it I get the error, "Invalid file handle"?
Thanks,
Did you check the return value of the fopen call as you've been told? You haven't shown that, nor the call to this function.
How about reading posts #13 and #15 in this very thread?
I tried post 13+15 but the program keeps closing the same way with the same error.
I changed my code back a bit , which goes through the process of updating and thinks it updates but when i look at the binary file after, it doesnt update, but it gives no errors and seems like it works fine.
Code:void binupdaterecord(FILE *cfPtr) { int patient; char patdoc[ 30 ]; struct pat_record Patient = { 0, "", 0, "" }; if ( ( cfPtr = fopen( "binpatient.dat", "rb" ) ) == NULL ) { printf( "File could not be opened.\n" ); } else { printf("Enter patient number to update: "); scanf("%d", &patient); fseek ( cfPtr,( ( patient -1) * sizeof (struct pat_record )), SEEK_SET); fread (&Patient, sizeof (struct pat_record), 1, cfPtr); if ( Patient.pat_no == 0 ){ printf("Patient #%d has no info.\n", patient );} else { printf ("%d %s %d %s\n\n", Patient.pat_no, Patient.name, Patient.dob, Patient.doctor ); printf("Enter new doctor:"); scanf("%s", patdoc); strcpy(Patient.doctor, patdoc); printf ("%d %s %d %s\n\n", Patient.pat_no, Patient.name, Patient.dob, Patient.doctor); fseek( cfPtr, (patient - 1 ) * sizeof (struct pat_record), SEEK_SET); fwrite(&Patient, sizeof ( struct pat_record), 1, cfPtr); } fclose( cfPtr ); } }
figured it out i had "rb" in instead of "rb+"
Thanks