CFile写入中文,正常显示并换行

来源:互联网 发布:程序员就是堆砌代码吗 编辑:程序博客网 时间:2024/05/12 19:55

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();

例子:

//Exception Handling//打开文件是一件可能产生许多exception的动作#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;}}END_CATCHreturn 0;}

打开hello.txt文件


参考:http://topic.csdn.net/u/20090630/18/bf331840-559a-4f4c-84ab-0df73b0d431b.html

原创粉丝点击