CFile f(_T("D:\\11\\0.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate);
f.SeekToEnd();
CString a = _T("D:\\11\\0.txt ") + str; // 錯誤:拼接的是檔案路徑而非內容
f.Write(a, a.GetLength() * sizeof(TCHAR));
開發者誤以為 str 會自動寫入,實際上將「目標檔案路徑」與內容拼接後寫入,導致檔案內容錯誤。
CFile f(_T("D:\\11\\0.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate);
f.SeekToEnd();
f.Write(str, str.GetLength() * sizeof(TCHAR)); // 直接寫入讀取到的字串內容
f.Close();
若需在內容前加入分隔符或換行,請對 str 進行字串操作後再寫入。
CFile::modeNoTruncate 確保不清空原有內容。SeekToEnd() 定位至檔案末尾實現追加。sizeof(TCHAR) 計算,支援 Unicode/ANSI 專案。