471,850 Members | 1,017 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 8441
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
aboka
reply views Thread by aboka | last post: by

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.