Thread: Update a file in C

  1. #1
    Registered User
    Join Date
    Jun 2007

    Update a file in C

    Hi all,

    I'm getting an annoying problem...I have been able to append data at the end of a file without any problem. But now I'm trying to open a text file, reading some of its data and trying to modify or update some of its data...but I'm finding it really hard to do I haven't been able to do it at all after couple of trials

    For example, let say there is the following contents in the text file:

    Name: John
    Age: 26
    Sex: Male

    Now I want to change the Age and put it to 31. How do I do that?? Please help!! What code to use??

    I want to keep the original file...that is I have to update the original one itself.

    I'm getting confused with the Mode as well..."r", "w", "a", "r+", "w+", "a+". Which one should I use?? I was trying "w+" and "w"...but doesn't work

    Thanking you in advance.
    Last edited by flyjason; 06-06-2007 at 03:30 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Show what you tried and didn't work.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    How's this for an algorithm?

    1) Open file for reading.
    2) Open a new file for writing.
    3) As you read in data, write it out to the new file, altering what you wish to change.
    4) Close both files.
    5) Delete original file.
    6) Rename new file to the name of the original file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  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