多次覆盖文件,使用fstream安全

时间:2014-01-24 13:39:42

标签: c++ fstream ofstream

fstream会多次覆盖文件会导致任何问题吗?

1 个答案:

答案 0 :(得分:1)

不,它不会。试试这个,你不会遇到任何问题。

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

int main(){

    ofstream out;

    for(int cnt = 0; cnt < 100; ++cnt){
        out.open("file.txt");
        out << "This will be written 100 times, and erased 99 times.";
        // Uncomment the lines below if you want to see each change
        //out << " Run#: " << cnt;
        //cin.get();
        out.close();
    }

    return 0;
}

免责声明:我没有尝试运行此代码。如果有任何语法错误,请道歉。