Pregunta

Will fstream overwriting a file multiple times cause any problems?

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top