Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

update part of a binary file but dont overwrite whole file

  • 21-03-2012 6:16pm
    #1
    Closed Accounts Posts: 11


    Hi,

    Im wondering..is there a way to update a record in a binary file rather than overwriting the whole file?

    For example, lets say I have a binary file containing 5 records - each record has a name and age.

    what i want to do is to open the file, put the file pointer to the record that is to be updated (lets say the third record), read the new data into a record and write that record to the file - overwriting the third record in the file.

    Is there a way I can do this?

    So far, I have been able to display the contents of the record the user wants to update with this code:

    fseek(pointer,sizeof(struct rec)*(record_pos), SEEK_SET);

    then i read into a record structure
    scanf(............);
    scanf(..............);

    and then write this record to the file:

    fwrite(&myrecord, sizeof(struct rec), 1, pointer);



    I have tried initially opening the file using the:

    pointer=fopen("dfile.bin", "ab+");

    and also

    pointer=fopen("dfile.bin", "wb+");

    and also

    pointer=fopen("dfile.bin", "w");

    But all with no luck

    cheers

    squinchy


Comments

  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Make sure your program actually has permissions to write the file. If using windows, certain dirs like program files etc are specially protected.


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    a is append, you can't use that, w creates an empty file for writing and w+ an empty file that is read write.

    Try rb+ and check if the pointer is NULL, it would be if you couldn't open the file with the desired access.


Advertisement