开发者

how to delete contents of a file from main body of c++

I wanted to know if I can delete the values (string or int) written in a file from main. i.e. erase开发者_如何学Go entire data in a text file and make it empty like before If yes, how?


Just overwrite it with a new, empty file:

#include <fstream>

std::ofstream ofs("myfile.txt");
ofs.close();


You can use ios_base::trunc openmode with ofstream:

(truncate) Any current content is discarded, assuming a length of zero on opening.

Example,

std::ofstream ofile("filename.txt", ios_base::trunc);

//work with ofile

ofile.close();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜