Thread: Overwriting Parts of a File

  1. #1
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69

    Overwriting Parts of a File

    Hi everyone

    Ive been trying to overwrite selected records in a text file.
    If i try and edit the first record in the file some parts of the old record are overwritten and other parts are left behind! If I try and overwrite any record other than record1 nothing at all is written to the file. (and no errors are reported :/ )

    Ive attached the program, and the 2 text files. The 2 text files need to be placed into a folder called "Data" in order to work.
    Attached Files Attached Files

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't just overwrite text files that way. If you want to modify / insert into the middle of a text file, this is what you have to do:

    1) Read everything into a buffer that is before what you want to replace.
    2) Read everything into a buffer that is past what you want to replace.
    3) Starting over again at the top of the file, write out the first buffer.
    4) Write whatever it is that you're changing / adding / inserting.
    5) Write out everything that is to come after it (the second buffer).

    The only way you can seek to a spot and write over it is in binary mode with fixed length records. And that only works if you're overwriting a specific spot with the exact same amount of data. You can't just insert anything into the middle of a file, or delete from the middle. You have to use the above steps to do it for you. Depending on binary mode or not, you can skip past all you want to leave there, then start at step 2.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    Ah i see.... oops

    Ill try the steps youve mentioned. Thanks

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I will just add a slight modification to what quzah said. Because some files (especially the type that you would want to do such a thing to) are too unreasonably large to load into memory every time you want to insert/modify/delete a record, you should instead:
    1) read each record in the source file and write it to a temp file
    2) when the record to be deleted/modified or point of insertion is detected, write the new record to the temp file
    3) read/write the remaining records
    4) delete the source file
    5) rename the temp file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM