niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
how can i use fstream object to clear file's content?
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
horneye
*nix forums beginner


Joined: 24 May 2006
Posts: 2

PostPosted: Fri Jul 21, 2006 7:52 am    Post subject: how can i use fstream object to clear file's content? Reply with quote

After i use fstream to open a file, how can i use the fstream object
to clear file's content?
Back to top
Alan Johnson
*nix forums Guru Wannabe


Joined: 14 May 2005
Posts: 206

PostPosted: Fri Jul 21, 2006 8:34 am    Post subject: Re: how can i use fstream object to clear file's content? Reply with quote

horneye wrote:
Quote:
After i use fstream to open a file, how can i use the fstream object
to clear file's content?


std::ofstream("filename") ;

Just opening it as such should be sufficient to cause the data to be
truncated.

--
Alan Johnson
Back to top
Richard Herring
*nix forums Guru


Joined: 21 Feb 2005
Posts: 336

PostPosted: Fri Jul 21, 2006 11:10 am    Post subject: Re: how can i use fstream object to clear file's content? Reply with quote

In message <e9q3hq$6kr$1@news.Stanford.EDU>, Alan Johnson
<alanwj@no.spam.stanford.edu> writes
Quote:
horneye wrote:
After i use fstream to open a file, how can i use the fstream object
to clear file's content?


std::ofstream("filename") ;

Just opening it as such should be sufficient to cause the data to be
truncated.

True, there will now be an empty file called "filename", but it depends

on the underlying operating system whether that is the same thing as
"clearing" an existing file's contents. On some OS's the previous
version of the file may still exist. Whether that distinction is
important depends on the OP's application.

Except for this special case of "truncate to zero length" (which may
actually be implemented as "replace with a new empty file") , the C++
file model doesn't allow you to reduce the size of an existing file,
only to overwrite or extend it.

--
Richard Herring
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 2:05 pm | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts CORBA object used in Vector niks C++ 0 Fri Jul 21, 2006 7:45 am
No new posts CORBA object used in Vector niks C++ 1 Fri Jul 21, 2006 7:40 am
No new posts Bug#379054: ITP: lisaac -- Lisaac is the first object-ori... picca frederic devel 0 Thu Jul 20, 2006 9:30 pm
No new posts ImportError: libclntsh.so.10.1: cannot open shared object... gmax2006 python 2 Thu Jul 20, 2006 7:05 pm
No new posts Memory Layout of a Object mailursubbu@gmail.com C++ 3 Thu Jul 20, 2006 2:24 pm

Personal Loans | Debt Consolidation | Mortgage | Mortgage Calculator | Loans
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1129s ][ Queries: 16 (0.0417s) ][ GZIP on - Debug on ]