Domanda

Will fstream overwriting a file multiple times cause any problems?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top