向文件写多种语言的问题

look_think 2008-04-15 06:40:10
有些内容,有中文、英文、日文,还有一些古怪的字符。

放在wstring里,在调试器里看没问题,但是用fwprintf写之后,文件里除了英文之外都是乱码。

不知道有什么好办法。

谢谢
...全文
113 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
look_think 2008-04-15
  • 打赏
  • 举报
回复
非常感谢各位,问题已经解决。

我用 _wfopen_s(&fp, “test.xml”,L"wt+, ccs=UTF-16LE"); 的方式写成功了。

虽然原因不明,但是一时也没精力进一步研究了,以后再说。
cnzdgs 2008-04-15
  • 打赏
  • 举报
回复
“4楼”是我吗?
到底是除了“英文都是乱码”还是“除了英文都是好的”?
感觉我的办法是行还是不行?
记事本创建的文件,最开始的两字节用0xff、0xfe表示该文件是Unicode编码;fopen用文本方式打开文件时,会对读写的数据做一些处理,有可能会把数据搞错。
arong1234 2008-04-15
  • 打赏
  • 举报
回复
有几个要点
1. 文件需要以二进制方式打开,避免0d0a被转换为0a
2. 文件开头必须写入0xFF, 0xFE两个字节
3. 你得字符串必须真的以unicode编码才行,不是放在wstring里就可以的
look_think 2008-04-15
  • 打赏
  • 举报
回复
谢谢3楼,不过无效。
谢谢4楼,我这里除了英文都是好的,感觉上你的办法可能不行,能否详述原理?

cnzdgs 2008-04-15
  • 打赏
  • 举报
回复
用二进制方式创建文件,文件开头需要写入0xff、0xfe两个字节,然后再写Unicode字符串(也是按二进制数据写入)。
look_think 2008-04-15
  • 打赏
  • 举报
回复
谢谢楼上二位,此种方法拿短字符串试验没问题。

多了又出现问号,正在研究。
Kudeet 2008-04-15
  • 打赏
  • 举报
回复
fwprintf需要一个FILE句柄,关键是看你创建FILE句柄时是否让这个文件以UNICODE形式存储数据了。类似:

FILE* fileHandle;

// Create an the xml file in text and Unicode encoding mode.
if ((fileHandle = _wfopen( L"_wfopen_test.xml",L"wt+,ccs=UNICODE")) == NULL) // C4996
// Note: _wfopen is deprecated; consider using _wfopen_s instead
{
...........
}

下面的示例来自MSDN:

// crt__wfopen.c
// compile with: /W3
// This program creates a file (or overwrites one if
// it exists), in text mode using Unicode encoding.
// It then writes two strings into the file
// and then closes the file.

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <wchar.h>

#define BUFFER_SIZE 50

int main(int argc, char** argv)
{
wchar_t str[BUFFER_SIZE];
size_t strSize;
FILE* fileHandle;

// Create an the xml file in text and Unicode encoding mode.
if ((fileHandle = _wfopen( L"_wfopen_test.xml",L"wt+,ccs=UNICODE")) == NULL) // C4996
// Note: _wfopen is deprecated; consider using _wfopen_s instead
{
wprintf(L"_wfopen failed!\n");
return(0);
}

// Write a string into the file.
wcscpy_s(str, sizeof(str)/sizeof(wchar_t), L"<xmlTag>\n");
strSize = wcslen(str);
if (fwrite(str, sizeof(wchar_t), strSize, fileHandle) != strSize)
{
wprintf(L"fwrite failed!\n");
}

// Write a string into the file.
wcscpy_s(str, sizeof(str)/sizeof(wchar_t), L"</xmlTag>");
strSize = wcslen(str);
if (fwrite(str, sizeof(wchar_t), strSize, fileHandle) != strSize)
{
wprintf(L"fwrite failed!\n");
}

// Close the file.
if (fclose(fileHandle))
{
wprintf(L"fclose failed!\n");
}
return 0;
}

用户 昵称 2008-04-15
  • 打赏
  • 举报
回复
同楼上。
ouyh12345 2008-04-15
  • 打赏
  • 举报
回复
unicode

写入文件时,转换成多字节的
读出来后,再转换成宽字符

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧