解決CFile 寫入中文後換行
阿新 • • 發佈:2019-02-19
CFile 正常寫入中文
CFile myFile; BOOL isopen=myFile.Open(filename, CFile::modeReadWrite|CFile::modeCreate | CFile::typeBinary|CFile::shareDenyNone); if(!isopen) AfxMessageBox("不能開啟檔案!"); CFile myFile(); CString EditContent; m_EditText->GetWindowText(EditContent); LPCTSTR str = EditContent.GetBuffer( EditContent.GetLength()); myFile.Write(str,EditContent.GetLength()*sizeof(CHAR)); myFile.Close();
例子:
#include <afx.h> #include <iostream.h> int main(){ CString str1="中國\r\n"; LPCTSTR s1=str1.GetBuffer(str1.GetLength()); CString str2="人民\r\n"; LPCTSTR s2=str2.GetBuffer(str2.GetLength()); CString str3="五湖四海\r\n"; LPCTSTR s3=str3.GetBuffer(str3.GetLength()); TRY { CFile file("hello.txt",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary); file.Write(s1,str1.GetLength()*sizeof(CHAR)); file.Write(s2,str2.GetLength()*sizeof(CHAR)); file.Write(s3,str3.GetLength()*sizeof(CHAR)); file.Close(); } CATCH (CFileException, e) { switch (e->m_cause) { case CFileException::accessDenied: TRACE("File Access Denied\n"); break; case CFileException::badPath: TRACE("Invalid Path\n"); break; case CFileException::diskFull: TRACE("Disk Full\n"); break; case CFileException::fileNotFound: TRACE("File Not Found\n"); break; case CFileException::hardIO: TRACE("Hardware Error\n"); break; case CFileException::lockViolation: TRACE("Attemp to lock region already locked \n"); break; case CFileException::sharingViolation: TRACE("Sharing Violation -load share.exe\n"); break; case CFileException::tooManyOpenFiles: TRACE("Too Many Open Files\n"); break; } } str1.ReleaseBuffer(); str2.ReleaseBuffer(); str3.ReleaseBuffer(); END_CATCH return 0; }