我想使用boost文件系统读取/写入具有unicode文件名的文件,在Windows上使用boost语言环境(mingw)(最后应该是平台独立的).
这是我的代码:
#include <boost/locale.hpp>
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
namespace fs = boost::filesystem;
#include <string>
#include <iostream>
int main() {
std::locale::global(boost::locale::generator().generate(""));
fs::path::imbue(std::locale());
fs::path file("äöü.txt");
if (!fs::exists(file)) {
std::cout << "File does not exist" << std::endl;
}
fs::ofstream(file, std::ios_base::app) << "Test" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在fs::exists真正检查与名称的文件äöü.txt.但是书面文件有名字äöü.txt.
阅读给出了同样的问题.使用fs::wofstream也没有帮助,因为这只是处理广泛的输入.
如何使用C++ 11和boost来解决这个问题?
编辑:发布错误报告:https://svn.boost.org/trac/boost/ticket/9968
澄清赏金: Qt很简单,但我想要一个只使用C++ 11和Boost,没有Qt和没有ICU的跨平台解决方案.
使用boost :: locale文档中的示例代码,我无法获得以下内容以正确地标记中文文本:
using namespace boost::locale::boundary;
boost::locale::generator gen;
std::string text="???????";
ssegment_index map(word,text.begin(),text.end(),gen("zh_CN.UTF-8"));
for(ssegment_index::iterator it=map.begin(),e=map.end();it!=e;++it)
std::cout <<"\""<< * it << "\", ";
std::cout << std::endl;
Run Code Online (Sandbox Code Playgroud)
这将中华人民共和国分为七个不同的角色中/华/人/民/共/和/国,而不是中华/人民/共和国.在ICU的文档,这增大对编译声称,中国应努力开箱,并使用基于字典的分词器正确分割短语.在上面的代码中使用"ja_JP.UTF-8"语言环境中的示例日语测试短语"生きるか死ぬか,それが问题だ." 确实有效,但此标记化不依赖于字典,仅在汉字/假名上边界.
我在这里建议直接在ICU中尝试相同的代码,但结果是一样的.
UnicodeString text = "???????";
UErrorCode status = U_ZERO_ERROR;
BreakIterator* bi = BreakIterator::createWordInstance(Locale::getChinese(), status);
bi->setText(text);
int32_t p = bi->first();
while (p != BreakIterator::DONE) {
printf("Boundary at position %d\n", p);
p = bi->next();
}
delete bi;
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
我试图在逗号是小数分隔符的德国语言环境中一起使用 boost::locale 和 std::stod 。考虑这个代码:
boost::locale::generator gen;
std::locale loc(""); // (1)
//std::locale loc = gen(""); // (2)
std::locale::global(loc);
std::cout.imbue(loc);
std::string s = "1,1"; //float string in german locale!
double d1 = std::stod(s);
std::cout << "d1: " << d1 << std::endl;
double d2 = 2.2;
std::cout << "d2: " << d2 << std::endl;
Run Code Online (Sandbox Code Playgroud)
std::locale loc("") 创建正确的语言环境,输出为
d1: 1,1
d2: 2,2
Run Code Online (Sandbox Code Playgroud)
正如我所料。当我注释掉第 (1) 行和取消注释第 (2) 行时,输出是
d1: 1
d2: 2.2
Run Code Online (Sandbox Code Playgroud)
d2 的结果在意料之中。据我了解 boost::locale 希望我明确指定 d2 应该被格式化为一个数字并做
std::cout << "d2: " << boost::locale::as::number …Run Code Online (Sandbox Code Playgroud) 在Win 7 64位和VS2010上提升1.54 x64.编译为"Release x64"并运行以下代码:
#include <boost/locale/conversion.hpp>
std::wstring y = L"NoNseNSE";
std::wstring w = boost::locale::to_lower(y);
Run Code Online (Sandbox Code Playgroud)
抛出std::bad_cast异常.添加后没有任何变化(如其他地方所示):
std::locale mylocale("");
std::locale::global(mylocale);
Run Code Online (Sandbox Code Playgroud)
或to_lower(y)改为:to_lower(y, mylocale)或在环境中使用std::string而不是std::wstring或设置LANG.
目标是转换为小写意大利语UTF-8字.我没有找到这样的问题,所以我认为这是我的机器特定问题或升级库问题.顺便说一下,我从sourceforge下载了预编译的boost库(boost_1_54_0-msvc-10.0-64.exe).任何的想法?谢谢!马里奥
我目前正在尝试为 iOS 构建 Boost.Locale,但无法找到 iconv 库(我正在成功构建 Boost for iOS 的其他部分,例如thread或filesystem)。
我试图让 Boost.Build 自己找到它,我试图将变量设置ICONV_PATH为指向 iPhoneOS SDK iconv lib。检查 Boost.Locale 中的 Jamfile,我偶然发现了这条规则:
lib iconv
:
: <search>$(ICONV_PATH)/lib <link>shared <runtime-link>shared
:
: <include>$(ICONV_PATH)/include
;
Run Code Online (Sandbox Code Playgroud)
所以我认为设置-sICONV_PATH为/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk/usr就足够了,因为这个目录包含一个lib和一个include包含 iconv lib 和标头的文件夹,但 Boost 仍然找不到它,并吐出:
iconv (libc) : no
iconv (separate) : no
icu : no
icu (lib64) : no
Boost.Locale needs either iconv or ICU library to be built.
Run Code Online (Sandbox Code Playgroud)
请注意,我总是b2使用该--reconfigure …
我有一些读取 unicode 代码点的代码(在字符串 0xF00 中转义)。
由于我使用boost,我在推测以下是否是最佳(和正确)方法:
unsigned int codepoint{0xF00};
boost::locale::conv::utf_to_utf<char>(&codepoint, &codepoint+1);
Run Code Online (Sandbox Code Playgroud)
?