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

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

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值