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_CATCH
return 0;
}
打开hello.txt文件
参考:http://topic.csdn.net/u/20090630/18/bf331840-559a-4f4c-84ab-0df73b0d431b.html