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

H

horneye

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

Alan Johnson

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

std::eek:fstream("filename") ;

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

Richard Herring

Alan Johnson said:
std::eek:fstream("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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top