Thread: modify txt file in C

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    1

    modify txt file in C

    hi everyone,
    i met some problem in this.
    i used fseek to locate position prepared to be written.
    for example,
    fseek(stream,16,seek_set)
    then
    fputs(time,stream)
    while time will be users input.

    the content of the txt file to be modified


    aabb, ccddeeff, 30gghhiijjkk


    obvious the string 30(at byte position 16)
    is going to be changed.
    but if users input were 3000
    then gg(position 18) will be overwrote.
    i will get the result
    aabb, ccddeeff, 3000hhiijjkk


    but all i need is


    aabb, ccddeeff, 3000gghhiijjkk


    or EVEN users input were 300000
    i still need the result to be


    aabb, ccddeeff, 300000gghhiijjkk


    how to do it?
    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You read the file line by line
    You change each line to suit
    You write the line to a new temporary file.

    When you're done, close both files, delete the original and rename the temporary.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM