new to fstream

F

Flzw

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
 
K

Karl Heinz Buchegger

Flzw said:
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.
 
F

Flzw

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...
 
T

Thomas Matthews

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

John Harrison

Flzw said:
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
 
F

Flzw

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....
 

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

Similar Threads

fstream Buffers 26
fstream File i/o 1
fstream 6
Understanding fstream seeking 2
New to programming 3
Error reason for fstream failures 5
New to python 4
fstream - write a file 3

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top