how to modify a txt file

Discussion in 'C Programming' started by Jerry, Jan 31, 2005.

  1. Jerry

    Jerry Guest

    Hi, everybody:

    I want to modify a portion of a txt file. The way I do it is to
    open the file in read mode, and create a new file, and delete the old
    file, and rename the new file.
    I am wondering if this is a common practice of modifying a file in
    C, is there any other way to do it?
    Thank you all for your attention.


    --Jerry
     
    Jerry, Jan 31, 2005
    #1
    1. Advertising

  2. Jerry

    Jacob Oost Guest

    Jerry wrote:
    > Hi, everybody:
    >
    > I want to modify a portion of a txt file. The way I do it is to
    > open the file in read mode, and create a new file, and delete the old
    > file, and rename the new file.
    > I am wondering if this is a common practice of modifying a file in
    > C, is there any other way to do it?
    > Thank you all for your attention.
    >
    >
    > --Jerry
    >


    Couldn't you just open a new file for reading or writing?

    fptr = fopen("filename", "r+");

    Of course you'd do the standard error-checking. I'm new to this but
    isn't that what you want? According to my book, "r+" opens an existing
    file for reading or writing.

    --

    ----- BEGIN GEEK CODE BLOCK -----
    Version 3.1
    GAT d? !s !a C++++ UL+ P L++ E- W+ N+ o-- K- w--
    O- !M !V PS-- PE++ Y+ PGP- t++>++++* 5? !X-- R- tv b++ DI+ D++
    G e !h !r !y
    ...... END GEEK CODE BLOCK ----
     
    Jacob Oost, Jan 31, 2005
    #2
    1. Advertising

  3. Jerry

    Jerry Guest

    Thanks, if it is just adding stuff at the end of an existing file, I'd
    use your approach. But modifying the txt involves deleting things in a
    file, and "inserting" things in the middle of a file. I am wondering if
    there is a way to delete things in a file. Meanwhile, I dont think
    "write" is as same as "insert", cause the file pointer does not care
    what's already in there, and it will just erease things in the existing
    file. therefore, if we want to use r+ mode, we need to take care of a
    lot of things.
     
    Jerry, Jan 31, 2005
    #3
  4. Jerry

    CBFalconer Guest

    Jerry wrote:
    >
    > I want to modify a portion of a txt file. The way I do it is to
    > open the file in read mode, and create a new file, and delete the
    > old file, and rename the new file.
    > I am wondering if this is a common practice of modifying a file in
    > C, is there any other way to do it?


    Yes, in any language. However you might think about how to recover
    if any of those steps fail, and how to detect those failures.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Jan 31, 2005
    #4
  5. Jerry

    Jerry Guest

    Thank you very much.

    CBFalconer wrote:
    > Jerry wrote:
    > >
    > > I want to modify a portion of a txt file. The way I do it is to
    > > open the file in read mode, and create a new file, and delete the
    > > old file, and rename the new file.
    > > I am wondering if this is a common practice of modifying a file in
    > > C, is there any other way to do it?

    >
    > Yes, in any language. However you might think about how to recover
    > if any of those steps fail, and how to detect those failures.
    >
    > --
    > "If you want to post a followup via groups.google.com, don't use
    > the broken "Reply" link at the bottom of the article. Click on
    > "show options" at the top of the article, then click on the
    > "Reply" at the bottom of the article headers." - Keith Thompson
     
    Jerry, Jan 31, 2005
    #5
    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:
    804
    John Carnahan
    Jul 18, 2003
  2. Steve
    Replies:
    3
    Views:
    26,998
    Steve
    Aug 25, 2006
  3. Sameen
    Replies:
    2
    Views:
    447
    Victor Bazarov
    Aug 29, 2005
  4. modify txt file in C

    , Dec 11, 2007, in forum: C Programming
    Replies:
    2
    Views:
    417
    Mark McIntyre
    Dec 11, 2007
  5. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,580
    Roedy Green
    Sep 15, 2011
Loading...

Share This Page