Thread: Updating a file

  1. #1

    Updating a file

    i have a problem with updating a file
    first the program read the file then save it in memory after that sort the records and ask user to input which record the user wants to update
    after find it and update the record
    how can i put it back in the same position in the file?
    after open and read the file first time then close it
    i open file again to put the updated record back in the same positon i use this


    then write it back


    it seems like it doesn't save it back in the same position it was
    do i have to use fseek? how to use it?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    You could use fseek(), you just need to calculate the number of bytes to offset before you start writing. This would be the sizeof the struct, against the number of structs before the one you want to write.

    Or you could rewrite the complete file (providing you have it all in memory).

    I see a small problem with your code, you are using "r+" to open the file. If you are going to write structs with fwrite (and read with fread), you should use a binary mode.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. 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