环境是vs2005
CStdioFile myfile;
myfile.Open( _T("c:\\a.txt"), CFile::modeCreate|CFile::modeWrite );
CString str = _T("abc\n");
myfile.WriteString( str );
CString strTemp = _T("你好");
myfile.WriteString( strTemp );
myfile.close();
项目-> 属性->常规->字符集 设置的是使用Unicode字符集。
为什么字母能写进去,汉字写不进去。怎么解决。
或者在MFC中用什么能解决往txt文件中一次写汉字,一次写字符的问题。
8 个解决方案
#1
CStdioFile myfile;
myfile.Open( _T("c:\\a.txt"), CFile::modeCreate|CFile::modeWrite );
char* old_locale = _strdup( setlocale(LC_CTYPE,NULL) );
setlocale( LC_CTYPE, "chs" );
CString str = _T("abc\n");
myfile.WriteString( str );
CString strTemp = _T("你好");
myfile.WriteString( strTemp );
setlocale( LC_CTYPE, old_locale );
free( old_locale );
myfile.Close();
myfile.Open( _T("c:\\a.txt"), CFile::modeCreate|CFile::modeWrite );
char* old_locale = _strdup( setlocale(LC_CTYPE,NULL) );
setlocale( LC_CTYPE, "chs" );
CString str = _T("abc\n");
myfile.WriteString( str );
CString strTemp = _T("你好");
myfile.WriteString( strTemp );
setlocale( LC_CTYPE, old_locale );
free( old_locale );
myfile.Close();
#2
这个好像只能转成ANSI再写(WideCharToMultiByte)
可以参考:
http://blog.csdn.net/a32132100/archive/2011/05/17/6425837.aspx
http://blog.csdn.net/MagicFuzzX/archive/2011/04/23/6347291.aspx
可以参考:
http://blog.csdn.net/a32132100/archive/2011/05/17/6425837.aspx
http://blog.csdn.net/MagicFuzzX/archive/2011/04/23/6347291.aspx
#3
mark 一下
#6
用CFile类
#7
1这个确实不能写进去
2去GOOGLE,有写好的扩展类
3用CFile
2去GOOGLE,有写好的扩展类
3用CFile
#8
用CFile类
#1
CStdioFile myfile;
myfile.Open( _T("c:\\a.txt"), CFile::modeCreate|CFile::modeWrite );
char* old_locale = _strdup( setlocale(LC_CTYPE,NULL) );
setlocale( LC_CTYPE, "chs" );
CString str = _T("abc\n");
myfile.WriteString( str );
CString strTemp = _T("你好");
myfile.WriteString( strTemp );
setlocale( LC_CTYPE, old_locale );
free( old_locale );
myfile.Close();
myfile.Open( _T("c:\\a.txt"), CFile::modeCreate|CFile::modeWrite );
char* old_locale = _strdup( setlocale(LC_CTYPE,NULL) );
setlocale( LC_CTYPE, "chs" );
CString str = _T("abc\n");
myfile.WriteString( str );
CString strTemp = _T("你好");
myfile.WriteString( strTemp );
setlocale( LC_CTYPE, old_locale );
free( old_locale );
myfile.Close();
#2
这个好像只能转成ANSI再写(WideCharToMultiByte)
可以参考:
http://blog.csdn.net/a32132100/archive/2011/05/17/6425837.aspx
http://blog.csdn.net/MagicFuzzX/archive/2011/04/23/6347291.aspx
可以参考:
http://blog.csdn.net/a32132100/archive/2011/05/17/6425837.aspx
http://blog.csdn.net/MagicFuzzX/archive/2011/04/23/6347291.aspx
#3
mark 一下
#4
#5
#6
用CFile类
#7
1这个确实不能写进去
2去GOOGLE,有写好的扩展类
3用CFile
2去GOOGLE,有写好的扩展类
3用CFile
#8
用CFile类