By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

using ofstream to alter file contents WITHOUT overwriting them

P: n/a
is there any way this can be done? I've looked at the help files and
checked out as many tutorials as i could find on the net (as always) but no
joy.

thanks
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Stewart wrote:
is there any way this can be done? I've looked at the help files and
checked out as many tutorials as i could find on the net (as always) but no
joy.


What does "alter without overwriting" mean?
Jul 22 '05 #2

P: n/a

"Stewart" <rs********@blueyonder.co.uk> wrote in message
news:Yk******************@fe1.news.blueyonder.co.u k...
is there any way this can be done? I've looked at the help files and
checked out as many tutorials as i could find on the net (as always) but
no joy.

thanks


If you mean inserting or removing pieces of a file, then the answer is no.
Files (in any language) don't work like that.

john

Jul 22 '05 #3

P: n/a
ah - sorry for the lack of clarity in my last post. it was pretty late when
i worte this and i was really tired from trying to solve this prob.

what i meant was suppose i had a text file with a string like:
"halo 2 is out on 11th novemeber"

now if i were to move the file poitner along a bit and insert some text
(let's say "asgujdsgf") then, you'd have something like this as a result:
halo 2 is out on 11asgujdsgfber

what I WANT to do is insert a string into the text in a text file, without
overwriting the pre-existing text, like this:
"halo 2 is out on 11th asgujdsgnovemeber"

is this at all possible?

thanks again
Jul 22 '05 #4

P: n/a
Stewart wrote:
ah - sorry for the lack of clarity in my last post. it was pretty late when
i worte this and i was really tired from trying to solve this prob.

what i meant was suppose i had a text file with a string like:
"halo 2 is out on 11th novemeber"

now if i were to move the file poitner along a bit and insert some text
(let's say "asgujdsgf") then, you'd have something like this as a result:
halo 2 is out on 11asgujdsgfber

what I WANT to do is insert a string into the text in a text file, without
overwriting the pre-existing text, like this:
"halo 2 is out on 11th asgujdsgnovemeber"

is this at all possible?


Right... No. File streams are essentially a mapping of file contents on
your external storage. AFAIK, no external storage allows "insertions"
except perhaps a very particular ones (a stack of punch-cards, maybe?).
So, since in general there is no support for insertions, ofstream, being
a generic file stream, doesn't have any mechanism in it either.

V
Jul 22 '05 #5

P: n/a
ah i see. thanks for your help victor
Jul 22 '05 #6

P: n/a

"Stewart" <rs********@blueyonder.co.uk> wrote in message
news:ND*****************@fe2.news.blueyonder.co.uk ...
ah - sorry for the lack of clarity in my last post. it was pretty late
when i worte this and i was really tired from trying to solve this prob.

what i meant was suppose i had a text file with a string like:
"halo 2 is out on 11th novemeber"

now if i were to move the file poitner along a bit and insert some text
(let's say "asgujdsgf") then, you'd have something like this as a result:
halo 2 is out on 11asgujdsgfber

what I WANT to do is insert a string into the text in a text file, without
overwriting the pre-existing text, like this:
"halo 2 is out on 11th asgujdsgnovemeber"

is this at all possible?

thanks again


The standard method is to read the file in, writing out to a new file all
the text up to the "insertion point", then writing out the text you're
inserting, then writing out the rest of the file. (But you're writing to a
different file, you understand. You can always rename the file(s), of
course, so that it looks like you're writing to the "same" file.)

-Howard
Jul 22 '05 #7

P: n/a

"Howard" <al*****@hotmail.com> wrote in message
news:EP*******************@bgtnsc05-news.ops.worldnet.att.net...

"Stewart" <rs********@blueyonder.co.uk> wrote in message
news:ND*****************@fe2.news.blueyonder.co.uk ...
ah - sorry for the lack of clarity in my last post. it was pretty late
when i worte this and i was really tired from trying to solve this prob.

what i meant was suppose i had a text file with a string like:
"halo 2 is out on 11th novemeber"

now if i were to move the file poitner along a bit and insert some text
(let's say "asgujdsgf") then, you'd have something like this as a result: halo 2 is out on 11asgujdsgfber

what I WANT to do is insert a string into the text in a text file, without overwriting the pre-existing text, like this:
"halo 2 is out on 11th asgujdsgnovemeber"

is this at all possible?

thanks again
The standard method is to read the file in, writing out to a new file all
the text up to the "insertion point", then writing out the text you're
inserting, then writing out the rest of the file. (But you're writing to

a different file, you understand. You can always rename the file(s), of
course, so that it looks like you're writing to the "same" file.)


One possible approach for single file implementation would be:

- copy string to circular buffer
- open and io stream for read/write
- find insertion point in io stream
- loop while !eof
- read char from stream
- push back char to buffer
- (over)write char to stream from front of circular buffer
- pop front
- loop while !buffer.empty()
- write char to stream from front of circular buffer
- pop front

Any comments from those more familiar with streams? Performance relative to
other methods would certainly need to be measured. I could see using this
approach when inserting relatively small string near the tail of a very
large file.

Jeff F
Jul 22 '05 #8

P: n/a
yeah it looks like i'll have to do summat like that. thanks a lot for all
of your help.
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.