2014-01-24 40 views
0

fstream是否會覆蓋文件多次導致任何問題?多次覆蓋文件,安全fstream

+0

這太寬泛了。你特別擔心什麼問題? – woz

+0

我很擔心文件被損壞。 –

+0

@StephenJacob如果您的計算機在文件被覆蓋時破壞文件,則需要獲取新計算機或至少一個新的存儲設備。 – molbdnilo

回答

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

聲明:我沒有嘗試運行此代碼。道歉,如果有任何語法錯誤。