Thread: updating binary file

  1. #1
    zach
    Guest

    updating binary file

    When I change a record in a binary file, I damage other records in the file. Does one need to copy records to an other file, add the modified record, and continue the copy? Then rename the file (if that can be done in c)? I would like to know of a better way. There doesn't seem to be an update file mode. Thank you. Zach.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    There doesn't seem to be an update file mode.
    Sure there is, where did you get your documentation for fopen()?

    Looks like your understanding is lacking, post your code so we can see what you're doing incorrectly.

  3. #3
    zach
    Guest
    Quote Originally Posted by jimblumberg View Post
    Sure there is, where did you get your documentation for fopen()?

    Looks like your understanding is lacking, post your code so we can see what you're doing incorrectly.
    Thank you for telling me in this way that there is a mode that does the job. I will continue enjoying my lacking understanding.
    Last edited by zach; 09-05-2019 at 03:25 PM.

  4. #4
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Red face

    When I change a record in a binary file, I damage other records in the file.
    say what now ? Elaborate ?
    "without goto we would be wtf'd"

  5. #5
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    zach its hard to tell what you're talking about, but it looks like you are overcomplicating the matter.

    The concept of "updating a file" is basically just reading the file into memory, modifying it, then writing the output to a file. Your program needs to have a way to track the records so that it does not corrupt them... Are you using a linked-list?
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  6. #6
    zach
    Guest
    Asymtotic, hi, sorry to have been unclear. The program (financial analysis) has been completed, thanks to you all ! , your patience in particular, and is running a treat.

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by zach View Post
    When I change a record in a binary file, I damage other records in the file. Does one need to copy records to an other file, add the modified record, and continue the copy? Then rename the file (if that can be done in c)? I would like to know of a better way. There doesn't seem to be an update file mode. Thank you. Zach.
    It really depends on what constitutes a "record" in your file.

    1) If all the records are fixed length, then you can update the file in place, although, the second method method would be safer.

    2) If the records are variable length, (Or you want to make a backup file) then you would need to read each record from the original file, updating the records that need updating, and writing each record to a new file. Renaming the original file with a backup name/extension, then renaming the new file to the original name.

    I would always recommend the second option.

  8. #8
    zach
    Guest
    Quote Originally Posted by rstanley View Post
    It really depends on what constitutes a "record" in your file.

    1) If all the records are fixed length, then you can update the file in place, although, the second method method would be safer.

    2) If the records are variable length, (Or you want to make a backup file) then you would need to read each record from the original file, updating the records that need updating, and writing each record to a new file. Renaming the original file with a backup name/extension, then renaming the new file to the original name.

    I would always recommend the second option.
    Thank you for responding. With much gratitude for the help I have been getting: the application has been completed and is running fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-06-2013, 11:39 PM
  2. Replies: 9
    Last Post: 05-25-2011, 02:59 AM
  3. Updating a binary file
    By jaybeeb in forum C Programming
    Replies: 26
    Last Post: 04-29-2008, 08:37 AM
  4. Updating file name
    By gazsux in forum C Programming
    Replies: 2
    Last Post: 04-24-2003, 03:11 PM
  5. updating a file
    By toad in forum C Programming
    Replies: 4
    Last Post: 06-04-2002, 11:39 PM

Tags for this Thread