Originally Posted by
master5001
Code:
long pat_max = fseek(fPtr, 0, SEEK_END) / sizeof(struct pat_record);
// your code goes here
scanf("%d", &pat_no);
if(pat_no > 0 && pat_no < pat_max)
// more code
This made the program crash when I selected "edit" didnt even get to enter a number?
Originally Posted by
master5001
Code:
if(!fPtr)
{
fprintf(stderr, "Invalid file handle.")
return;
}
Not sure exactly where to put this?? (bit of a novice at C)
Originally Posted by
swoopy
Code:
> fread( &patient, sizeof( struct pat_record ), 1, fPtr );
I would be sure the read was successful by checking the return value:
Code:
if (fread( &patient, sizeof( struct pat_record ), 1, fPtr ) != 1)
{
printf("Error reading record %d\n", pat_no);
return;
}
Also be sure you opened the file in binary mode:
Code:
if ((fPtr = fopen(filename, "rb+") == NULL)
Put all this in but still getting the same error!?
Here is my code as it stands now
Code:
void binupdaterecord(FILE *fPtr){
int pat_no;
char transaction[20];
struct pat_record patient = { 0 , "" , 0 , "" };
if ((fPtr = fopen("binpatient.dat", "rb+") == NULL));
printf("Enter the number of the patient to be updated\n");
printf(":");
scanf("%d",&pat_no);
fseek( fPtr,( pat_no - 1 ) * sizeof( struct pat_record ), SEEK_SET );
if(!fPtr)
{
fprintf(stderr, "Invalid file handle.");
return;
}
if (fread( &patient, sizeof( struct pat_record ), 1, fPtr ) !=1);
{
printf("Error reading record %d\n", pat_no);
return;
}
if ( patient.pat_no == 0 )
printf( "Patient number %d has no information.\n", pat_no );
else {
printf( "%d%s%d%s\n\n", patient.pat_no,patient.name,patient.dob,patient.doctor);
printf("Enter change:");
scanf("%20s",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 );
}
}
Thanks for the help so far!