Question

Will fstream overwriting a file multiple times cause any problems?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top