text file indexing

Discussion in 'C++' started by electrixnow, Feb 9, 2006.

  1. electrixnow

    electrixnow Guest

    in VC++ Express Edi

    Is their a way to replace one line of TEXT in an existing text file. I
    now open it then
    read in the data, once I have found somthing that needs updating, I
    need to overwrite
    that one line only and continue reading the rest of the file to check
    for other line that
    may need updates.

    How can this be done with fstream.

    I am reading about stream pointers but have not yet found anything on
    overwriting a
    line of text on an existing file.

    If I could overwrite the line just after a getline was done that would
    be great. Would need
    to index -1 line write over the line and then would I need to index +1
    lines for the next
    getline?
    Not sure how to do this.

    any comments or suggestions about the issue.

    Thanks
    electrixnow, Feb 9, 2006
    #1
    1. Advertising

  2. electrixnow wrote:
    > in VC++ Express Edi


    If you need VC++ specific solution, post to the VC++ newsgroup:
    'microsoft.public.vc.language'. Here it doesn't not matter what
    compiler/IDE you use, unless you're asking about a bug in the
    compiler.

    > Is their a way to replace one line of TEXT in an existing text file.


    If this is a question (questions usually end on a question mark), then
    the answer is "yes, but it depends on your definition of 'one line'".

    > I
    > now open it then
    > read in the data, once I have found somthing that needs updating, I
    > need to overwrite
    > that one line only and continue reading the rest of the file to check
    > for other line that
    > may need updates.
    >
    > How can this be done with fstream.


    Again, if this is a question, then the answer is "just like with C
    streams, you open one for reading, you open the other for writing, you
    read one until you find the data you need replaced, you write all that
    stuff before to the new stream, then you write the new data into the
    output, then continue the copying after the data that needed to be
    replaced".

    > I am reading about stream pointers but have not yet found anything on
    > overwriting a
    > line of text on an existing file.


    Because it's not a functionality of a stream. It's an algorithm that
    entirely depends on _your_ definition of "a line".

    > If I could overwrite the line just after a getline was done that would
    > be great.


    What do you mean by that?

    > Would need
    > to index -1 line write over the line and then would I need to index +1
    > lines for the next
    > getline?
    > Not sure how to do this.


    Another simple way is to read the whole file into a linked list of strings
    (where each string is a "line"), then replace the string that contains the
    "line" to be replaced with the new string, then write the whole list of
    strings back to the file (or better to a newly created file).

    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Feb 9, 2006
    #2
    1. Advertising

  3. electrixnow

    electrixnow Guest

    Thanks for the input. I have code that opens two files like you talked
    about. Open the original for reading, then opens new file for write,
    getline original, I use << to write to new. If any changes are needed
    in the data, then the new data is substituted to the new file for that
    line.

    What at really want to do is open the text file for read/write, use
    getline for text.
    Evaluate the line for changes, If any, and just change the line or data
    in the line
    at that point. After that I would use getline to eval the next line of
    text in file. That way
    I would only have the original file open and would not have to create a
    second.

    Thanks
    electrixnow, Feb 9, 2006
    #3
  4. electrixnow wrote:
    > Thanks for the input. I have code that opens two files like you talked
    > about. Open the original for reading, then opens new file for write,
    > getline original, I use << to write to new. If any changes are needed
    > in the data, then the new data is substituted to the new file for that
    > line.
    >
    > What at really want to do is open the text file for read/write, use
    > getline for text.
    > Evaluate the line for changes, If any, and just change the line or data
    > in the line
    > at that point. After that I would use getline to eval the next line of
    > text in file. That way
    > I would only have the original file open and would not have to create a
    > second.


    You have the original file like this (4 "lines" separated by, say, '\n'):
    ---------------------
    abc
    defghijk
    lmnopq
    rstuvwxyz
    ---------------------
    Now, you need to replace the second line with "111111111111111111111".
    What's going to happen?

    Or, you need to replace the second line with "123". What's the result?

    Think about it.

    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Feb 9, 2006
    #4
  5. electrixnow

    Jim Langston Guest

    "electrixnow" <> wrote in message
    news:...
    > Thanks for the input. I have code that opens two files like you talked
    > about. Open the original for reading, then opens new file for write,
    > getline original, I use << to write to new. If any changes are needed
    > in the data, then the new data is substituted to the new file for that
    > line.
    >
    > What at really want to do is open the text file for read/write, use
    > getline for text.
    > Evaluate the line for changes, If any, and just change the line or data
    > in the line
    > at that point. After that I would use getline to eval the next line of
    > text in file. That way
    > I would only have the original file open and would not have to create a
    > second.
    >
    > Thanks


    Works for binary files because the data is fixed length. That is, you could
    open the file in binary mode (not using << for input) and replace the
    characters "the" with "cat" because they are the same length. If, however,
    you try to replace "the" with "gator" what will happen is the "or" will
    overwrite the next 2 characters, whatever they happen to be. Of course you
    could read the next 2 characters first and after the "or" write them, but
    then they'd be overwritting characters, etc.. and you'd have to do that to
    the end of the file.

    Basically, if what you are replacing is not the same length as what is there
    now, you open the file for input, and another file for output. Read from
    input, write to output. That's the only easy way to change different length
    data.
    Jim Langston, Feb 9, 2006
    #5
  6. electrixnow

    electrixnow Guest

    Thanks thats what I will do or have done.
    electrixnow, Feb 9, 2006
    #6
  7. electrixnow wrote:
    > What at really want to do is open the text file for read/write, use
    > getline for text.
    > Evaluate the line for changes, If any, and just change the line or data
    > in the line


    As others have explained, you can do this OK if the replacement text is
    the same size as the original. What happens if the replacement is not?

    Well, in an ideal universe there would be a "insert_bytes( file, len )"
    and a "delete_bytes( file, len )" or something. But such an operation is
    not possible in any common file system as far as I know, and therefore
    libraries don't have such a thing by default.
    Aaron Lawrence, Feb 15, 2006
    #7
    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. C
    Replies:
    0
    Views:
    476
  2. Emin
    Replies:
    4
    Views:
    392
    Paul McGuire
    Jan 12, 2007
  3. Foodbank

    Indexing a Text File

    Foodbank, Dec 18, 2005, in forum: C Programming
    Replies:
    5
    Views:
    381
    Walter Roberson
    Dec 19, 2005
  4. Skybuck Flying
    Replies:
    30
    Views:
    1,059
    Bill Reid
    Sep 19, 2011
  5. C
    Replies:
    3
    Views:
    207
    Manohar Kamath [MVP]
    Oct 17, 2003
Loading...

Share This Page