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

From: Thomas Matthews (Thomas_MatthewsHatesSpam_at_sbcglobal.net)
Date: 11/06/03


Date: Thu, 06 Nov 2003 21:12:41 GMT

Paul LAURENT wrote:
> 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 news:comp.lang.c++.
{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


Relevant Pages

  • Re: Using STLport with Microsoft Visual C++ 6
    ... >which is compiler vendor and OS independent. ... The STL was an algorithms library invented by Stepanov and Lee. ... So the STL, in a slightly modified form, is part of the C++ standard ... C++ FAQ: http://www.parashift.com/c++-faq-lite/ ...
    (microsoft.public.vc.stl)
  • Re: Kann hier auch zum Thema c++ gefragt werden ?
    ... Gemeint war die STL = Standard Lib, sorry, Info: ... one can now push and pop integers onto this stack. ... In Java hat ist man übrigens von einigen zentralen Klassen, ...
    (de.comp.lang.c)
  • Re: Visual Studio STL on Linux? Portable STL suggestion?
    ... STL freely available with a commercial licence for e.g. Linux? ... the C++ standard (in fact it was in large parts incorporated into the ... MFC so you couldn't legally compile it using WINE under a free OS. ... STLport is a free project that also aims to provide a portable standard ...
    (microsoft.public.vc.stl)
  • Re: Any F2003 help translating C++ map syntax?
    ... >>part of the standard language, but makes a mockery of comparing ... > I sometimes miss convenient containers in the standard Fortran. ... That means that the standard STL might not be as ... > a hash table, if I remember correctly. ...
    (comp.lang.fortran)
  • Re: Deriving from templates
    ... The Standard headers are not ... The reason to "obfuscate" it is to protect from possible name ... > rate STL documentation as the worst there is, ...
    (comp.lang.cpp)