2017-10-11 35 views
-3

我想要实现在C蟒蛇码++相同的结果:C++得到了用户首选编码或本地编码

import locale 
encoding = locale.getpreferredencoding() 

encoding是我的电脑“CP936”(窗口10,中国简体)

我试试下面的C++代码:

test1.cpp:

#include <iostream> 
#include <clocale> 

using namespace std; 

int main() 
{ 
    setlocale(LC_ALL, ""); 
    string lc_all = std::setlocale(LC_ALL, NULL); 
    cout << lc_all << endl; // prints Chinese (Simplified)_China.936 
    return 0; 
} 

嗯...我仍然不满意,所以我转向boost::locale(boost_1_65_1),但有一些奇怪的行为。

测试2.cpp:

#include <iostream> 
#include <boost/locale.hpp> 

using namespace std; 

int main() 
{ 
    boost::locale::generator gen; 
    string encoding = std::use_facet<boost::locale::info>(gen("")).encoding(); 
    cout << encoding << endl; 
    return 0; 
} 

问题1:的代码将导致连接错误,除非包括另一个头:#include <boost/filesystem.hpp>,为什么呢?是否有任何文件可以使用boost::locale还必须包含boost::filesystem

1>libboost_locale-vc140-mt-1_65_1.lib(generator.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" ([email protected]@[email protected]@[email protected]@XZ) 
1>libboost_locale-vc140-mt-1_65_1.lib(date_time.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" ([email protected]@[email protected]@[email protected]@XZ) 
1>libboost_locale-vc140-mt-1_65_1.lib(localization_backend.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" ([email protected]@[email protected]@[email protected]@XZ) 
1>libboost_locale-vc140-mt-1_65_1.lib(lcid.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" ([email protected]@[email protected]@[email protected]@XZ) 
1>libboost_locale-vc140-mt-1_65_1.lib(generator.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" ([email protected]@[email protected]@[email protected]@XZ) 
1>libboost_locale-vc140-mt-1_65_1.lib(date_time.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" ([email protected]@[email protected]@[email protected]@XZ) 
1>libboost_locale-vc140-mt-1_65_1.lib(localization_backend.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" ([email protected]@[email protected]@[email protected]@XZ) 
1>libboost_locale-vc140-mt-1_65_1.lib(lcid.obj) : error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" ([email protected]@[email protected]@[email protected]@XZ) 

问题2:通过添加#include <boost/filesystem.hpp>,程序运行,但encoding是 “UTF-8”,为什么呢?我认为这是“CP936”,与test1.cpp

问题的结果是一致的:什么++获得的C语言环境编码(实现与Python的locale.getpreferredencoding()相同的结果)最好的主意。非常感谢!

环境: win10 64位,vs2015,boost_1_65_1(Windows预编译,lib64下-MSVC-14.0)

+0

先阅读文档! – Silencer

+0

@Silencer你怎么看我没看过这个文档?您可以提供更有用的评论,例如所谓的“文档”的正确位置 – oz1

+1

由于http://www.boost.org/doc/libs/1_62_0/libs/locale/doc/html/examples .html显示你需要做的事情。 – UKMonkey

回答

0

蟒蛇locale.getpreferredencoding()的等价:

std::string getpreferredencoding() 
{ 
    std::string strCodePage = boost::locale::util::get_system_locale(); 
    std::locale loc = boost::locale::generator().generate(strCodePage); 
    return std::use_facet<boost::locale::info>(loc).encoding(); 
} 
0

问题1:代码将导致连接错误,除非包括另一个头的:#include ,为什么?有没有任何文件可以说使用boost :: locale还必须包含boost :: filesystem?

自动链接不按预期工作。这是一个向boost库开发人员报告的错误。在这种特殊情况下,我要去猜测¹Boost系统应该已经被链接,并且Boost Locale/Boost系统头文件不包含适当的编译指示。

包括提高文件系统发生解决链接错误,因为它确实有库编译

¹,因为你无法显示的链接错误

+0

感谢您指出,添加了链接错误。 – oz1