472,362 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,362 software developers and data experts.

using ofstream to alter file contents WITHOUT overwriting them

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
8 3199
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

"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
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
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
ah i see. thanks for your help victor
Jul 22 '05 #6

"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
4
by: Gactimus | last post by:
Here is a program that encodes and decodes a text file. What I need to do is write a C++ program that requests 3 different file names. One filename is for the source file to be encoded, another is...
0
by: idesilva | last post by:
Hi, I have an app that generates a text log at a very high rate. Currently I use an ofstream object to write the log. I would like to control when 'exactly' the log is written to the disk. Also...
15
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the...
2
by: Paul LAURENT | last post by:
Hi everybody, I am using the STL "ofstream" class. I open a file using "ofstream" in update at the end mode ("ate"), I can read and write my file correctly. => What I would like to do is...
4
by: adamrobillard | last post by:
Hi, I have always used fopen and FILE* to load and save structures to file. I am trying to convert all the older code to use proper C++ calls... the following code works properly but I would...
7
by: Serge Rielau | last post by:
Hi all, Following Ian's passionate postings on problems with ALTOBJ and the alter table wizard in the control center I'll try to explain how to use ALTOBJ with this thread. I'm not going to get...
10
true911m
by: true911m | last post by:
This is a simple walkthrough to get PyInstaller up and running. I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.