Thread: Updating a binary file

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It's probably crashing somewhere in the calling function, because any code below the fread is never executed.

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok... so here goes what I meant

    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 );
         }
     }
    [edit]The indentation was killing me, plus I optimized your printf()'s a bit.[/edit]
    Last edited by master5001; 04-23-2008 at 05:58 PM.

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  5. #20
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I agree 100&#37;, 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.

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Hi again, sorry was away for a while, master5001, thanks for the code, with it I get the error, "Invalid file handle"?
    Thanks,

  7. #22
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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.

  8. #23
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Quote Originally Posted by rags_to_riches View Post
    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.
    I am a bit of a noob when it comes to C, how exactly would I check the return value of the fopen call?

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jaybeeb View Post
    I am a bit of a noob when it comes to C, how exactly would I check the return value of the fopen call?
    Check that the file-pointer returned is not NULL, perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #25
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    How about reading posts #13 and #15 in this very thread?

  11. #26
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    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 );
            }
    }

  12. #27
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    figured it out i had "rb" in instead of "rb+"
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM