Modifying portion of binary files?

This is a discussion on Modifying portion of binary files? within the C Programming forums, part of the General Programming Boards category; Hi, I have a large binary file and I only need my code to modify a small portion of it ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    Question Modifying portion of binary files?

    Hi,

    I have a large binary file and I only need my code to modify a small portion of it in the middle. What is the best way to read in, modify, and then save to a new file?

    My knowledge of C is basic (just one module at university) so please try and explain things which may not be obvious.

    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm unsure what you're looking for, but I'll give you a big Welcome! to the board, anyway!

    Recommended way:
    Open the file in binary mode (read and write mode),
    read the data into a large buffer, writing out the parts that you don't want changed, to a temp file (which you have opened in binary mode as well).
    locate the part that you want changed, when it's in the buffer,
    make the change in the buffer, and finish writing out the correct data to the temp file.
    confirm the new file has the right size.
    delete the original file
    rename the temp file with the original file's name.

    Although you CAN write data to a binary file directly and make the change, it's fraught with the possibility of an error occurring, and wrecking a part, or all of, the data file. If the data file is all backed up already, it may not be a problem to take this risk - but that seems to never be the case, does it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary files: copying jpeg or video files
    By cfanatic in forum C Programming
    Replies: 5
    Last Post: 07-19-2012, 08:17 AM
  2. Need help with last portion of code.
    By CodeNLoad in forum C Programming
    Replies: 3
    Last Post: 07-02-2011, 09:46 PM
  3. Modifying Text Files
    By quantum_coffee in forum Tech Board
    Replies: 5
    Last Post: 11-18-2006, 01:15 AM
  4. How to get only a certain portion of the string
    By chops11 in forum C Programming
    Replies: 13
    Last Post: 10-14-2005, 12:39 PM
  5. Deleting first portion of a string
    By MethodMan in forum C Programming
    Replies: 5
    Last Post: 02-27-2004, 02:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21