如何在C ++中将文件编码格式设置为UTF8 [英] How to set file encoding format to UTF8 in C++

查看:771
本文介绍了如何在C ++中将文件编码格式设置为UTF8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的软件的要求是,包含导出数据的文件的编码应为UTF8。但是当我将数据写入文件时,编码始终是ANSI。 (我使用Notepad ++来检查这一点。)



我现在正在做的是尝试手动转换文件,读取它,将其转换为UTF8,一个新文件。



std :: string

inputFile std :: ifstream

pOutputFile FILE *

  // ... 

if(inputFile.is_open())
{
while(inputFile.good())
{
getline(inputFile,line);

// 1
DWORD dwCount = MultiByteToWideChar(CP_ACP,0,line.c_str(),-1,NULL,0);
wchar_t * pwcharText;
pwcharText = new wchar_t [dwCount];

// 2
MultiByteToWideChar(CP_ACP,0,line.c_str(),-1,pwcharText,dwCount);

// 3
dwCount = WideCharToMultiByte(CP_UTF8,0,pwcharText,-1,NULL,0,NULL,NULL);
char * pText;
pText = new char [dwCount];

// 4
WideCharToMultiByte(CP_UTF8,0,pwcharText,-1,pText,dwCount,NULL,NULL);

fprintf(pOutputFile,pText);
fprintf(pOutputFile,\\\
);

delete [] pwcharText;
delete [] pText;
}
}

// ...

不幸的是,编码仍然是ANSI。我搜索一段时间的解决方案,但我总是遇到解决方案通过MultiByteToWideChar和WideCharToMultiByte。然而,这似乎不工作。



我也在这里看到一个解决方案,但大多数UTF8问题处理C#和PHP的东西。


<在VC ++ 2010中的Windows是可能的(迄今为止还没有实现在GCC,据我所知)使用本地化facet std :: codecvt_utf8_utf16(即在C ++ 11)。 cppreference.com 的示例代码提供了您需要阅读/写UTF-8文件。

  std :: wstring wFromFile = _T(

A requirement for my software is that the encoding of a file which contains exported data shall be UTF8. But when I write the data to the file the encoding is always ANSI. (I use Notepad++ to check this.)

What I'm currently doing is trying to convert the file manually by reading it, converting it to UTF8 and writing the text to a new file.

line is a std::string
inputFile is an std::ifstream
pOutputFile is a FILE*

// ...

if( inputFile.is_open() )
{
    while( inputFile.good() )
    {
        getline(inputFile,line);

        //1
        DWORD dwCount = MultiByteToWideChar( CP_ACP, 0, line.c_str(), -1, NULL, 0 );
        wchar_t *pwcharText;
        pwcharText = new wchar_t[ dwCount];

        //2
        MultiByteToWideChar( CP_ACP, 0, line.c_str(), -1, pwcharText, dwCount );

        //3
        dwCount = WideCharToMultiByte( CP_UTF8, 0, pwcharText, -1, NULL, 0, NULL, NULL );
        char *pText;
        pText = new char[ dwCount ];

        //4
        WideCharToMultiByte( CP_UTF8, 0, pwcharText, -1, pText, dwCount, NULL, NULL );

        fprintf(pOutputFile,pText);
        fprintf(pOutputFile,"\n");

        delete[] pwcharText;
        delete[] pText;
    }
}

// ...

Unfortunately the encoding is still ANSI. I searched a while for a solution but I always encounter the solution via MultiByteToWideChar and WideCharToMultiByte. However, this doesn't seem to work. What am I missing here?

I also looked here on SO for a solution but most UTF8 questions deal with C# and php stuff.

解决方案

On Windows in VC++2010 it is possible (not yet implemented in GCC, as far as i know) using localization facet std::codecvt_utf8_utf16 (i.e. in C++11). The sample code from cppreference.com has all basic information you would need to read/write UTF-8 file.

std::wstring wFromFile = _T("
                        

这篇关于如何在C ++中将文件编码格式设置为UTF8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆