There are two ways to go about it. If the file is small enough, then read the data into memory. Modify it however you want, then write the modified data back to the harddrive.
If it's too large for using memory, then open the file for reading, open a temporary file for writing. You then copy over whatever data you need to to the temp file. Then delete the original, and rename the temp file.
If it's too large for using memory, then open the file for reading, open a temporary file for writing. You then copy over whatever data you need to to the temp file. Then delete the original, and rename the temp file.