문제

Will fstream overwriting a file multiple times cause any problems?

도움이 되었습니까?

해결책

No, it will not. Try this, you won't get any problems.

#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;
}

Disclaimer: I did not try to run this code. Apologies if there are any syntax errors.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top