C语言FILE不能创建unicode编码文本文件,文件头加0xff和0xfe之后fwprintf输出的unicode文本都变成乱码

void SaveResult(wchar_t* savepath)
{
FILE *f=_wfopen(savepath,L"w");
if(f==NULL)
{
return;
}
sort(g_wordinfo.begin(),g_wordinfo.end(),CompareStr);
LPWORDINFO lpword=NULL;
LPWORDINFO lpsubword=NULL;
LPPosInfo lppos=NULL;
fwprintf(f,L"%-20s %-4s %s\r\n",L"单词",L"次数",L"出处(文本号*页* 行)");
vector<LPWORDINFO>::const_iterator wordi=g_wordinfo.begin();
vector<LPWORDINFO>::const_iterator wordsubi=NULL;
for(;wordi!=g_wordinfo.end();wordi++)
{
lpword=(*wordi);
if(lpword!=NULL)
{
int sumnum=0;
if(lpword->m_ss.size()>0)
{
for(wordsubi=lpword->m_ss.begin();wordsubi!=lpword->m_ss.end();wordsubi++)
{
lpsubword=(*wordsubi);
sumnum+=lpsubword->m_pos.size();
}
}
fwprintf(f,L"%-20s %-4d\r\n",lpword->m_wd,lpword->m_pos.size()+sumnum);
fwprintf(f,L" %-16s %-4d ",L"-∮",lpword->m_pos.size());
for(lvpos::const_iterator ipos=lpword->m_pos.begin();ipos!=lpword->m_pos.end();ipos++)
{
lppos=(*ipos);
fwprintf(f,L"%2d*%2d*%2d ",lppos->m_text,lppos->m_page,lppos->m_line);
}
fwprintf(f,L"\r\n");
if(lpword->m_ss.size()>0)
{
for(wordsubi=lpword->m_ss.begin();wordsubi!=lpword->m_ss.end();wordsubi++)
{
lpsubword=(*wordsubi);
fwprintf(f,L" %-16s %-4d ",lpsubword->m_wd,lpsubword->m_pos.size());
for(lvpos::const_iterator ipos=lpsubword->m_pos.begin();ipos!=lpsubword->m_pos.end();ipos++)
{
lppos=(*ipos);
fwprintf(f,L"%2d*%2d*%2d ",lppos->m_text,lppos->m_page,lppos->m_line);
}
fwprintf(f,L"\r\n");
}
}
}
}
fclose(f);
}
这段代码应该怎么改啊?请各位大侠不吝赐教,谢谢。

第1个回答  2012-07-29
你到底用写 还是用ASCII码写文件,你打开时记事本使用的是ASCII码。
用二进制方式创建文件,文件开头需要写入0xff、0xfe两个字节,然后再写Unicode(也是按二进制数据写入)。
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 <>
#include <stddef.h>
#include <>
#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;
}

这个本回答被提问者和网友采纳
第2个回答  2012-07-24
我不会

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网