Updating a file

This is a discussion on Updating a file within the C Programming forums, part of the General Programming Boards category; i have a problem with updating a file first the program read the file then save it in memory after ...

  1. #1
    Unregistered
    Guest

    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

    f=fopen("a:\\Country.txt","r+");

    then write it back

    fwrite(&cty,sizeof(cty),1,f);

    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?
    Thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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