modify txt file in C

Discussion in 'C Programming' started by chaobrenda@gmail.com, Dec 11, 2007.

  1. Guest

    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
    , Dec 11, 2007
    #1
    1. Advertising

  2. Lew Pitcher Guest

    On Dec 11, 1:10 pm, wrote:
    > 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)


    You do realize that for a text stream, the offset must either be zero,
    or be the value returned by a previous successful call to ftell() on
    the same stream, don't you?

    > then
    > fputs(time,stream)
    > while time will be users input.

    [snip]
    > how to do it?
    > thanks


    In a nutshell, you cannot do it with one file. While you can replace
    in place data, the replacement data must be the same size as the data
    to be replaced. You cannot "insert" data without overwriting other
    data elements.

    You will have to write your program to create a second file into which
    you will copy data from the first file. You will first copy the data
    prior to the data to be changed. You will then write the new data, and
    finally, you will copy the data following the data to be changed. The
    first file will not change; the unaffected data from the first file
    will be copied to the second file, in sequence with the inserted data
    Lew Pitcher, Dec 11, 2007
    #2
    1. Advertising

  3. wrote:
    > hi everyone,
    > i met some problem in this.
    > i used fseek to locate position prepared to be written.


    This is a FAQ - 12.30, and also read 19.14

    And in fact, please read the FAQ entirely...

    --
    Mark McIntyre

    CLC FAQ <http://c-faq.com/>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
    Mark McIntyre, Dec 11, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Carnahan

    File Access error - writing to .txt file

    John Carnahan, Jul 18, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    800
    John Carnahan
    Jul 18, 2003
  2. Steve
    Replies:
    3
    Views:
    26,963
    Steve
    Aug 25, 2006
  3. Jerry

    how to modify a txt file

    Jerry, Jan 31, 2005, in forum: C Programming
    Replies:
    4
    Views:
    253
    Jerry
    Jan 31, 2005
  4. Sameen
    Replies:
    2
    Views:
    431
    Victor Bazarov
    Aug 29, 2005
  5. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,521
    Roedy Green
    Sep 15, 2011
Loading...

Share This Page