- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请注意,这不是与How to open an std::fstream (ofstream or ifstream) with a unicode filename? 相同的问题.那个问题是关于一个 unicode 文件名,这个问题是关于一个 unicode 文件内容。
我需要使用 ifstream 打开一个 UTF-8 unicode 文件(包含西类牙语字符)。在 Linux 下这没问题,但在 Windows 下它是。
bool OpenSpanishFile(string filename)
{
ifstream spanishFile;
#ifdef WINDOWS
spanishFile.open(filename.c_str(),ios::binary);
#endif
if (!spanishFile.is_open()) return false;
spanishFile.clear();
spanishFile.seekg(ios::beg);
while (spanishFile.tellg()!=-1)
{
string line="";
getline(spanishFile,line);
//do stuff
cout << line << endl;
}
return true;
}
我在 Linux 下编译它:
i586-mingw32msvc-g++ -s -fno-rtti test.cpp test.exe
然后在wineconsole test.exe
中运行。
输出包含各种奇怪的字符,因此它会尝试以不同的方式打开 unicode 文件。
我在互联网上搜索了很多有关如何以这种方式打开 unicode 文件的信息,但我无法让它工作。
有人知道适用于 mingw 的解决方案吗?非常感谢您。
最佳答案
很可能(不清楚所提供的代码是否是真实代码)您看到垃圾的原因是 Windows 中的 std::cout
默认以非 UTF-8 格式呈现其结果控制台窗口。
要正确检查您是否正在正确读取 UTF-8 文件,只需将所有输入收集到一个字符串中,将其从 UTF-8 转换为 UTF-16 wstring
,然后使用MessageBoxW
(或广泛的直接控制台输出)。
以下 UTF-8 → UTF-16 转换函数可以很好地与 Visual C++ 12.0 配合使用:
#include <codecvt> // std::codecvt_utf8_utf16
#include <locale> // std::wstring_convert
#include <string> // std::wstring
auto wstring_from_utf8( char const* const utf8_string )
-> std::wstring
{
std::wstring_convert< std::codecvt_utf8_utf16< wchar_t > > converter;
return converter.from_bytes( utf8_string );
}
不幸的是,即使它只使用标准的 C++11 功能,它也无法使用 MinGW g++ 4.8.2 进行编译,但希望你有 Visual C++(毕竟它是免费的)。
作为替代方案,您可以使用 Windows API MultiByteToWideChar
编写转换函数。
例如,以下代码可以很好地与 g++ 4.8.2 一起使用 -D USE_WINAPI
:
#undef UNICODE
#define UNICODE
#include <windows.h>
#include <shellapi.h> // ShellAbout
#ifndef USE_WINAPI
# include <codecvt> // std::codecvt_utf8_utf16
# include <locale> // std::wstring_convert
#endif
#include <fstream> // std::ifstream
#include <iostream> // std::cerr, std::endl
#include <stdexcept> // std::runtime_error, std::exception
#include <stdlib.h> // EXIT_FAILURE
#include <string> // std::string, std::wstring
namespace my {
using std::ifstream;
using std::ios;
using std::runtime_error;
using std::string;
using std::wstring;
#ifndef USE_WINAPI
using std::codecvt_utf8_utf16;
using std::wstring_convert;
#endif
auto hopefully( bool const c ) -> bool { return c; }
auto fail( string const& s ) -> bool { throw runtime_error( s ); }
#ifdef USE_WINAPI
auto wstring_from_utf8( char const* const utf8_string )
-> wstring
{
if( *utf8_string == '\0' )
{
return L"";
}
wstring result( strlen( utf8_string ), L'#' ); // More than enough.
int const n_chars = MultiByteToWideChar(
CP_UTF8,
0, // Flags, only alternative is MB_ERR_INVALID_CHARS
utf8_string,
-1, // ==> The string is null-terminated.
&result[0],
result.size()
);
hopefully( n_chars > 0 )
|| fail( "MultiByteToWideChar" );
result.resize( n_chars );
return result;
}
#else
auto wstring_from_utf8( char const* const utf8_string )
-> wstring
{
wstring_convert< codecvt_utf8_utf16< wchar_t > > converter;
return converter.from_bytes( utf8_string );
}
#endif
auto text_of_file( string const& filename )
-> string
{
ifstream f( filename, ios::in | ios::binary );
hopefully( !f.fail() )
|| fail( "file open" );
string result;
string s;
while( getline( f, s ) )
{
result += s + '\n';
}
return result;
}
void cpp_main()
{
string const utf8_text = text_of_file( "spanish.txt" );
wstring const wide_text = wstring_from_utf8( utf8_text.c_str() );
//ShellAbout( 0, L"Spanish text", wide_text.c_str(), LoadIcon( 0, IDI_INFORMATION ) );
MessageBox(
0,
wide_text.c_str(),
L"Spanish text",
MB_ICONINFORMATION | MB_SETFOREGROUND
);
}
} // namespace my
auto main()
-> int
{
using namespace std;
try
{
my::cpp_main();
return EXIT_SUCCESS;
}
catch( exception const& x )
{
cerr << "!" << x.what() << endl;
}
return EXIT_FAILURE;
}
关于c++ - 如何在 Windows 下使用 mingw 使用 ifstream 打开 unicode 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23921261/
我是一名优秀的程序员,十分优秀!