C++: 利用remove删除文件中的内容

一般,如果发现要打开的文件中有内容,要删除的话,可以直接删除该文件,然后再创建一个。

不过也可以这样

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    string str = "temp.txt";
    ofstream ofs(str.c_str());
    if (ofs.good())
    {
        remove(str.c_str());
    }

   // 现在temp.txt中已经没有内容了,可以写你想要输入的东西啦!

   return 0;
}

猜你喜欢

转载自blog.csdn.net/tanmx219/article/details/82655927