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