How do I delete lines from a file using "ofstream" ?

P

Paul LAURENT

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 deleting some lines from the file which decreases
the size of the file.

For performance reasons, I DO NOT WANT to close and open the file each time I
want to delete some information in it (I know that you can overwrite the file
content by re-opening the file)

I only found methods which allow you to add some information but no methods for
deleting the information.

Any idea will be welcome !



Thanks in advance.



Paul
 
T

Thomas Matthews

Paul said:
Hi everybody,


I am using the STL "ofstream" class.
There is no STL, ofstream, or class in standard C. You may want the
folks across the hall in {However, the answer to your issue is still the same, see below}.

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 deleting some lines from the file which decreases
the size of the file.

For performance reasons, I DO NOT WANT to close and open the file each time I
want to delete some information in it (I know that you can overwrite the file
content by re-opening the file)
Yes, but overwriting does not guarantee adjusting the end of the file to
the last used record.

I only found methods which allow you to add some information but no methods for
deleting the information.

Any idea will be welcome !



Thanks in advance.



Paul

Unfortunately, there is no process in _standard_ C to truncate a file
by delete portions of it. The portable method is to copy the portions
of the old file to a new file and then delete the old file {you could
rename the new file at this point}.

However, your platform may have some specific functions to perform
what you want. Ask the experts in a newsgroup about your platform.

--
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.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

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

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top