_tfopen_s UNICODE 解决乱码问题
2014-06-06 22:19
465 查看
使用_tfopen_s和fopen_s时,稍不注意就会出现乱码问题;
_tfopen_s函数对应的CCS设置为UNICODE, UTF-8, andUTF-16LE均可打开编码为Unicode, UTF-8和Unicode big endian的.txt文件,不可打开编码为ANSI的.txt文件。
void CFileView::OnWrite()
{
// TODO: Add your command handler code here
FILE *pFile = 0;
if (!_tfopen_s(&pFile, TEXT("111.txt"), TEXT("w,ccs=UNICODE"))) //写入txt文件编码默认为ANSI
{
fwrite(TEXT("Today is the first day of NBA final game!"), 1,
sizeof(TEXT("Today is the first day of NBA final game!")), pFile);
}
//fseek(pFile, 0, SEEK_CUR);
//fwrite("Today is the first day of NBA final game!", 1,
//sizeof("Today is the first day of NBA final game!")+2, pFile);
fclose(pFile);
//fflush(pFile);
}
void CFileView::OnOpen()
{
// TODO: Add your command handler code here
FILE *pFile;
if (!_tfopen_s(&pFile, TEXT("111.txt"), TEXT("r,ccs=UNICODE"))) //打开文件
{
TCHAR str[100] = {0};
//memset(str, '\0', 100);
fread(str, 1, sizeof(str), pFile);
MessageBox(str);
fclose(pFile);
}
}
fopen_s函数可以打开ANSI,Unicode,UTF-8, Unicode big endian编码的.txt文件,但fwrite要加上TEXT();且操作完成后,.txt文件都保存为Unicode编码的。
_tfopen_s函数对应的CCS设置为UNICODE, UTF-8, andUTF-16LE均可打开编码为Unicode, UTF-8和Unicode big endian的.txt文件,不可打开编码为ANSI的.txt文件。
void CFileView::OnWrite()
{
// TODO: Add your command handler code here
FILE *pFile = 0;
if (!_tfopen_s(&pFile, TEXT("111.txt"), TEXT("w,ccs=UNICODE"))) //写入txt文件编码默认为ANSI
{
fwrite(TEXT("Today is the first day of NBA final game!"), 1,
sizeof(TEXT("Today is the first day of NBA final game!")), pFile);
}
//fseek(pFile, 0, SEEK_CUR);
//fwrite("Today is the first day of NBA final game!", 1,
//sizeof("Today is the first day of NBA final game!")+2, pFile);
fclose(pFile);
//fflush(pFile);
}
void CFileView::OnOpen()
{
// TODO: Add your command handler code here
FILE *pFile;
if (!_tfopen_s(&pFile, TEXT("111.txt"), TEXT("r,ccs=UNICODE"))) //打开文件
{
TCHAR str[100] = {0};
//memset(str, '\0', 100);
fread(str, 1, sizeof(str), pFile);
MessageBox(str);
fclose(pFile);
}
}
fopen_s函数可以打开ANSI,Unicode,UTF-8, Unicode big endian编码的.txt文件,但fwrite要加上TEXT();且操作完成后,.txt文件都保存为Unicode编码的。
void CFileView::OnWrite()</span>
{ // TODO: Add your command handler code here FILE *pFile = 0; if (!fopen_s(&pFile, ("1.txt"), ("w"))) //写入txt文件编码默认为ANSI { fwrite(("Today is the first day of NBA final game!"), 1, sizeof(("Today is the first day of NBA final game!")), pFile); } //fseek(pFile, 0, SEEK_CUR); //fwrite("Today is the first day of NBA final game!", 1, //sizeof("Today is the first day of NBA final game!")+2, pFile); fclose(pFile); //fflush(pFile); } void CFileView::OnOpen() { // TODO: Add your command handler code here FILE *pFile; if (!fopen_s(&pFile, ("1.txt"), ("r"))) //打开文件 { TCHAR str[100] = {0}; //memset(str, '\0', 100); fread(str, 1, sizeof(str), pFile); MessageBox(str); fclose(pFile); } }
相关文章推荐
- 关于 OpenSmtp 邮件标题过长后出现乱码问题的解决
- js window.open将参数传递到struts中出现的乱码问题解决
- unicode和多字节字符中CString转string方法,可解决中文乱码的问题
- 解决 iOS NSDictionary 输出中文字符”乱码”(Unicode编码)问题
- MFC 多字节和UNICODE的相互转换(解决中文utf8-ASCNII乱码问题)
- 解决Unicode的乱码问题
- 关于 OpenSmtp 邮件标题过长后出现乱码问题的解决
- Jsoncpp 中文汉字unicode乱码解决及标点问题解决
- 聊聊计算机中的编码(Unicode,GBK,ASCII,utf8,utf16,ISO8859-1等)以及乱码问题的解决办法
- window.open传递到服务器之后乱码问题的解决方法
- 关于window.open()中文传值乱码问题的解决方法
- ReadString在UNICODE下中文乱码的解决办法(Char型字符和Unicode字符的转换问题)
- 解决python使用open打开文件中文乱码的问题
- 关于解决乱码问题的一点探索之二(涉及Unicode(utf-16)和GBK)
- 关于OpenSmtp邮件标题过长,附件名,用户名出现乱码问题的终及解决Dll文件
- 关于OpenSmtp邮件标题过长后出现乱码问题的解决
- 聊聊计算机中的编码(Unicode,GBK,ASCII,utf8,utf16,ISO8859-1等)以及乱码问题的解决办法
- [UnicodeEncodeError]:Django中解决URL中文解释乱码问题
- 解决 iOS NSDictionary 输出中文字符”乱码”(Unicode编码)问题
- 解决PHP执行乱码、每次都要手动更改Firefox Unicode设置问题