我使用Visual Studio 2010 簡單向檔案輸出helloworld
CFile test_file;
test_file.Open(_T("./test.txt"), CFile::modeCreate|CFile::modeWrite);
test_file.Write("helloworld", sizeof("helloworld"));
test_file.Write("helloworld", strlen("helloworld"));
test_file.Write("helloworld", sizeof("helloworld")); // 有這一行就會出現亂碼,沒有就正常
test_file.Close();
uj5u.com熱心網友回復:
_T("./test.txt")_T(".\\test.txt")
uj5u.com熱心網友回復:
sizeof("helloworld\0")uj5u.com熱心網友回復:
sizeof("helloworld") 缺個 0 (sz)uj5u.com熱心網友回復:
string以‘\0‘結尾,sizeof要+1uj5u.com熱心網友回復:
3樓 有誤。應該是int n=sizeof("helloworld");//10+'0'
即比strlen 多了個 0;
這個 0 寫入 檔案 是 txt 檔案出現 亂碼。
uj5u.com熱心網友回復:
應該使用strlen,而不是sizeofuj5u.com熱心網友回復:
編碼格式的問題,當加入最后一行時,存盤的資料長度為32位,記事本誤認為unicode編碼而實際寫入檔案的是多位元組,如果長度不是16的倍數,或者每個字串都以‘\0’結尾,都不會決議出錯uj5u.com熱心網友回復:
// 記事本誤認為unicodeCFile test_file;
test_file.Open("test.txt", CFile::modeCreate|CFile::modeWrite);
test_file.Write("Ha", sizeof("Ha"));// 3 (+0)
test_file.Write("H", strlen("H"));// 1
test_file.Close();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/38758.html
標籤:基礎類
