【发布时间】:2018-04-15 09:43:19
【问题描述】:
我正在尝试读取和处理多个不同编码的文件。我应该只为此使用 STL。 假设我们有 iso-8859-15 和 UTF-8 文件。
在this SO 中回答说:
简而言之,对您来说更有趣的部分:
std::stream(stringstream,fstream,cin,cout) 有一个内部 locale-object,它与全局 C++ 语言环境的值匹配 创建流对象的时刻。正如std::in是 在调用 main 中的代码之前很久就创建了它,它拥有最多 可能是经典的 C 语言环境,无论您之后做什么。- 您可以确保 std::stream 对象具有所需的 通过调用语言环境
std::stream::imbue(std::locale(your_favorite_locale))。
问题在于,在这两种类型中,只有与首先创建的语言环境匹配的文件才能被正确处理。例如,如果 locale_DE_ISO885915 位于 locale_DE_UTF8 之前,则 UTF-8 中的文件不会正确附加到 string s 中,当我将 cout 取出时,我只会看到文件中的几行。
void processFiles() {
//setup locales for file decoding
std::locale locale_DE_ISO885915("de_DE.iso885915@euro");
std::locale locale_DE_UTF8("de_DE.UTF-8");
//std::locale::global(locale_DE_ISO885915);
//std::cout.imbue(std::locale());
const std::ctype<wchar_t>& facet_DE_ISO885915 = std::use_facet<std::ctype<wchar_t>>(locale_DE_ISO885915);
//std::locale::global(locale_DE_UTF8);
//std::cout.imbue(std::locale());
const std::ctype<wchar_t>& facet_DE_UTF8 = std::use_facet<std::ctype<wchar_t>>(locale_DE_UTF8);
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::string currFile, fileStr;
std::wifstream inFile;
std::wstring s;
for (std::vector<std::string>::const_iterator fci = files.begin(); fci != files.end(); ++fci) {
currFile = *fci;
//check file and set locale
if (currFile.find("-8.txt") != std::string::npos) {
std::locale::global(locale_DE_ISO885915);
std::cout.imbue(locale_DE_ISO885915);
}
else {
std::locale::global(locale_DE_UTF8);
std::cout.imbue(locale_DE_UTF8);
}
inFile.open(path + currFile, std::ios_base::binary);
if (!inFile) {
//TODO specific file report
std::cerr << "Failed to open file " << *fci << std::endl;
exit(1);
}
s.clear();
//read file content
std::wstring line;
while( (inFile.good()) && std::getline(inFile, line) ) {
s.append(line + L"\n");
}
inFile.close();
//remove punctuation, numbers, tolower...
for (unsigned int i = 0; i < s.length(); ++i) {
if (ispunct(s[i]) || isdigit(s[i]))
s[i] = L' ';
}
if (currFile.find("-8.txt") != std::string::npos) {
facet_DE_ISO885915.tolower(&s[0], &s[0] + s.size());
}
else {
facet_DE_UTF8.tolower(&s[0], &s[0] + s.size());
}
fileStr = converter.to_bytes(s);
std::cout << fileStr << std::endl;
std::cout << currFile << std::endl;
std::cout << fileStr.size() << std::endl;
std::cout << std::setlocale(LC_ALL, NULL) << std::endl;
std::cout << "========================================================================================" << std::endl;
// Process...
}
return;
}
正如您在代码中看到的,我尝试过使用global 和locale local variables,但无济于事。
另外,在How can I use std::imbue to set the locale for std::wcout? SO 回答中指出:
所以看起来确实有一个底层 C 库机制 应该首先使用 setlocale 启用以允许注入转换 正常工作。
这“晦涩”的机制是这里的问题吗?
在处理文件时是否可以在两种语言环境之间交替? 我应该灌输什么(cout、ifstream、getline?)以及如何灌输?
有什么建议吗?
PS:为什么和语言环境相关的一切都这么乱? :|
【问题讨论】:
-
"为什么和语言环境相关的一切都这么乱?" Overengineering 最好的。
-
@Eljay 不知何故,这样一个微不足道的任务必须有一个解决方法......
-
我会看看我是否能让你的代码工作,但说实话我放弃了 C++ 并在 Python 3 中完成了我的“多种编码的多个文本文件”。
-
@Eljay 我可以使用 python 将每个文件转换为 utf8 编码,然后将 c++ 中的文件作为 utf8 处理,但解决问题的方法有点冗长
-
最简单的方法是忘记 C++ 中曾经存在的语言环境并使用第三方库,例如 libiconv。
标签: c++ character-encoding locale ifstream wifstream