468,512 Members | 1,518 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

how can i use fstream object to clear file's content?

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

Jul 21 '06 #1
2 8149
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.

--
Alan Johnson
Jul 21 '06 #2
In message <e9**********@news.Stanford.EDU>, Alan Johnson
<al****@no.spam.stanford.eduwrites
>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
Jul 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by gukn9700 | last post: by
8 posts views Thread by Brandon McCombs | last post: by
3 posts views Thread by David Blasdell | last post: by
5 posts views Thread by derek | last post: by
9 posts views Thread by Someonekicked | last post: by
6 posts views Thread by wiso | last post: by
6 posts views Thread by Gaijinco | last post: by
6 posts views Thread by canilao | last post: by
1 post views Thread by Sachin Garg | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.