[問題] 如何輸出到檔案且不覆蓋原本的資料

看板C_and_CPP作者 (coolman)時間13年前 (2012/04/06 16:03), 編輯推噓3(304)
留言7則, 4人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 我會使用run 來產生一筆筆GPattern的資料,每當我產生一筆就要輸出到檔案, 但用一般書上的教學方法,輸出會覆蓋掉原先的資料。 因為我會將每一筆輸出的資料編排成新的string,所以似乎沒有辦法先存起來, 最後再一次輸出。 想請問有沒有方法在我的結構下,一次輸出一筆資料到檔案,但不覆蓋原本的資料。 謝謝:) 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) struct GPattern() { int gid; .... } class Example() { public: void run(string _filename, unsigned int _minsup); void PrintGPattern(GPattern&, unsigned int sup); .... }; Eample::run(string filename, unsigned int minsup) { for(...) { // some condition // generate one GPattern, and i want to ouput it PrintGPattern(gp, sup); } } Example::PrintGPattern(GPattern& gp, unsigned int sup) { // I want to ouput each GPattern to a .txt file } main() { Example example; example.run(); } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.107.163

04/06 16:18, , 1F
append mode?
04/06 16:18, 1F

04/06 16:29, , 2F
謝謝您!!!之前不知道可以用append mode 感謝!
04/06 16:29, 2F

04/06 17:08, , 3F
我覺得應該是把檔案先開好 全寫完再關掉 但不知道哪種好~
04/06 17:08, 3F

04/06 17:08, , 4F
有待高手仔細回應 我是覺得開一次檔 全部寫完再關檔會好點
04/06 17:08, 4F

04/06 17:57, , 5F
樓上未必.有時候是要做 log_err,這時反而適合寫一次關一次
04/06 17:57, 5F

04/06 19:09, , 6F
樓上這種情形只要寫完後沖一下 (flush) 應該就行了吧...
04/06 19:09, 6F

04/06 20:08, , 7F
嗯,忘了還可以沖,謝謝樓上。
04/06 20:08, 7F
文章代碼(AID): #1FVgDKQy (C_and_CPP)