new to fstream

Discussion in 'C++' started by Flzw, Sep 13, 2004.

  1. Flzw

    Flzw Guest

    Hello, I'm not sure how to do this,

    first, just to be sure, getline() reads and copy to the buffer up to the
    delim paramater, and then sets the get pointer after the delimiter right ?

    Now, let's say I have both get and put pointer at the start of a given
    string in a file.

    And I want to replace this string with another one, how do I do ?


    All the help I can find on iostream on msdn and google is very confusing...
    Thanks
     
    Flzw, Sep 13, 2004
    #1
    1. Advertising

  2. Flzw wrote:
    >
    > Hello, I'm not sure how to do this,
    >
    > first, just to be sure, getline() reads and copy to the buffer up to the
    > delim paramater, and then sets the get pointer after the delimiter right ?
    >
    > Now, let's say I have both get and put pointer at the start of a given
    > string in a file.
    >
    > And I want to replace this string with another one, how do I do ?
    >


    you open the input file
    you create and open an intermediate file

    you read text from the input and write it to the
    intermediate file until you reach the point where the
    substitution has to take place.

    You then read the original text from the input file *but*
    you write the replacement text instead to the intermediate file

    you then continue to read the input file and write it to
    the intermediate file until the end of input is reached.

    At this point you have 2 files:
    the original file
    the intermediate file, which is an exact copy of the original file
    with the exception of the replacement.

    You delete the original file
    and rename the intermediate file to the same name the original file had.

    Job done.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 13, 2004
    #2
    1. Advertising

  3. Flzw

    Flzw Guest


    > you open the input file
    > you create and open an intermediate file


    There's no way to do it with only one file open for both reading and wrting
    ? because I will have to do several (maybe many) replacements one by one, so
    copying the whole file x times, deleting then renaming looks like a lot of
    overhead...
     
    Flzw, Sep 13, 2004
    #3
  4. Flzw wrote:
    >>you open the input file
    >>you create and open an intermediate file

    >
    >
    > There's no way to do it with only one file open for both reading and wrting
    > ? because I will have to do several (maybe many) replacements one by one, so
    > copying the whole file x times, deleting then renaming looks like a lot of
    > overhead...
    >
    >


    You can overwrite the text, if and only if, the size of the old text is
    the same as the new text.

    The C++ language has no facilities for inserting text into the middle
    of a file. This has to do with the nature of sequential files. Imagine
    a long tape with files one after another with no room between them.
    In order to expand one file, all the other ones have to be moved further
    down the tape. Big process. So, a new file is created at the end of
    the tape containing the new data. The old file is marked as deleted.

    If you have a lot of insertions, you may want to create a linked list
    of the corrections. The original data is written to the new file until
    the first item in the list. Then the list item is written to the new
    file. More data from the original file is copied over until the next
    item in the list is reached, then it is written to the file. Repeat
    until the original file hits EOF.


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Sep 13, 2004
    #4
  5. "Flzw" <> wrote in message
    news:ci416c$4j5$...
    >
    > > you open the input file
    > > you create and open an intermediate file

    >
    > There's no way to do it with only one file open for both reading and

    wrting
    > ? because I will have to do several (maybe many) replacements one by one,

    so
    > copying the whole file x times, deleting then renaming looks like a lot of
    > overhead...
    >


    I've never heard of an file I/O system that supports insertion into the
    middle of a file.

    You should probably read the whole file into memory. If its too big for
    that, then I think you've got some work to do.

    john
     
    John Harrison, Sep 13, 2004
    #5
  6. Flzw

    Flzw Guest


    > You should probably read the whole file into memory. If its too big for
    > that, then I think you've got some work to do.


    Yes, that's what I do, don't know why it didn't come to mind earler.

    However, io streams classes look extremely complex compared to what they
    actually do....
     
    Flzw, Sep 13, 2004
    #6
    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. Marc Schellens

    vector of fstream

    Marc Schellens, Jul 15, 2003, in forum: C++
    Replies:
    2
    Views:
    395
    John Harrison
    Jul 16, 2003
  2. Marc Schellens

    fstream.open( name, mode) fails

    Marc Schellens, Jul 15, 2003, in forum: C++
    Replies:
    0
    Views:
    1,465
    Marc Schellens
    Jul 15, 2003
  3. Armando
    Replies:
    6
    Views:
    747
    Armando
    Jan 29, 2004
  4. roughtrader
    Replies:
    3
    Views:
    444
    James Kanze
    Feb 17, 2009
  5. Replies:
    2
    Views:
    465
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page