File Content Modification

Discussion in 'C++' started by Tinku, Apr 12, 2007.

  1. Tinku

    Tinku Guest

    Dear C++'ers

    I like to know how we can modify the contents in a file

    ex: There is a file.txt

    with the following data

    Weather=Cold
    WhatIDo=SleepOntheBed

    to be replaced with

    Weather=Sunny
    WhatIDo=GoToBeach

    The seekp function able to replace the contents but it also
    eliminating the further contents in the buffer. Is there any way we
    read only one line of the file ata time, adjust the size of this line
    to fit the new contents.
    For my application putting all the file contents in an array is not
    feasible.

    Hope I am clear
    Waiting for ur replies
    Cheers
    P
    Tinku, Apr 12, 2007
    #1
    1. Advertising

  2. On 12 Apr, 13:01, "Tinku" <> wrote:
    > Dear C++'ers
    >
    > I like to know how we can modify the contents in a file
    >
    > ex: There is a file.txt
    >
    > with the following data
    >
    > Weather=Cold
    > WhatIDo=SleepOntheBed
    >
    > to be replaced with
    >
    > Weather=Sunny
    > WhatIDo=GoToBeach
    >
    > The seekp function able to replace the contents but it also
    > eliminating the further contents in the buffer. Is there any way we
    > read only one line of the file ata time, adjust the size of this line
    > to fit the new contents.
    > For my application putting all the file contents in an array is not
    > feasible.


    An alternative is to open a second, temporary file, to which you write
    the changes and when done replace the original file with the temporary
    one.

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Apr 12, 2007
    #2
    1. Advertising

  3. Tinku wrote:
    > Dear C++'ers
    >
    > I like to know how we can modify the contents in a file
    >
    > ex: There is a file.txt
    >
    > with the following data
    >
    > Weather=Cold
    > WhatIDo=SleepOntheBed
    >
    > to be replaced with
    >
    > Weather=Sunny
    > WhatIDo=GoToBeach
    >
    > The seekp function able to replace the contents but it also
    > eliminating the further contents in the buffer. Is there any way we
    > read only one line of the file ata time, adjust the size of this line
    > to fit the new contents.
    > For my application putting all the file contents in an array is not
    > feasible.


    It would be easy if all the lines were the same length ! Remember the
    80 column text files from IBM mainframe days ?

    OK, so maybe not.

    An alternative is to rewrite the entire file.
    Gianni Mariani, Apr 12, 2007
    #3
  4. Tinku

    Tinku Guest

    On Apr 12, 2:01 pm, Gianni Mariani <> wrote:
    > Tinkuwrote:
    > > Dear C++'ers

    >
    > > I like to know how we can modify the contents in a file

    >
    > > ex: There is a file.txt

    >
    > > with the following data

    >
    > > Weather=Cold
    > > WhatIDo=SleepOntheBed

    >
    > > to be replaced with

    >
    > > Weather=Sunny
    > > WhatIDo=GoToBeach

    >
    > > The seekp function able to replace the contents but it also
    > > eliminating the further contents in the buffer. Is there any way we
    > > read only one line of the file ata time, adjust the size of this line
    > > to fit the new contents.
    > > For my application putting all the file contents in an array is not
    > > feasible.

    >
    > It would be easy if all the lines were the same length ! Remember the
    > 80 column text files from IBM mainframe days ?
    >
    > OK, so maybe not.
    >
    > An alternative is to rewrite the entire file.


    Thanks all for replying me...

    I missed one important requirement.
    I have to modify lines randomly in the file by taking those particular
    lines as parameters. There is one nasty function doing this stuff. It
    scans the file until that line has reached and try to adjust the file
    pointer to the appropriate position and modify the content there. So
    using a temp file could not be a feasible solution to my problem as
    only one global file has to be maintained throughout the software
    run.

    I like to know if there is a way to read one line at a time (ex:
    getline()) modify it and put the new content into this line without
    disturbing the content in the next line. Any suggestions/ ideas in
    this direction will be very much appreciated.

    Thanking you

    Cheers
    Tinku, Apr 13, 2007
    #4
    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. Andy Fish
    Replies:
    0
    Views:
    617
    Andy Fish
    Jul 20, 2004
  2. hazz
    Replies:
    6
    Views:
    49,547
    SkyUCHC
    Jun 9, 2010
  3. Manuel Bastioni

    Get the date of last modification of a file

    Manuel Bastioni, Sep 8, 2003, in forum: Python
    Replies:
    6
    Views:
    2,044
    Manuel Bastioni
    Sep 9, 2003
  4. Replies:
    0
    Views:
    276
  5. Replies:
    3
    Views:
    270
    Bengt Richter
    Jan 25, 2005
Loading...

Share This Page