text file indexing

E

electrixnow

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
 
V

Victor Bazarov

electrixnow said:
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
 
E

electrixnow

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
 
V

Victor Bazarov

electrixnow said:
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
 
J

Jim Langston

electrixnow said:
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.
 
A

Aaron Lawrence

electrixnow said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top