解决CFile 写入中文后换行

来源:互联网 发布:c语言经典编程题pdf 编辑:程序博客网 时间:2024/04/28 06:38

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










0 0
原创粉丝点击