Ich möchte das gleiche Ergebnis mit dem Python-Codes in C erreichen ++:C++ erhält die Benutzer bevorzugte Codierung oder Locale
import locale
encoding = locale.getpreferredencoding()
Die encoding
ist 'cp936' auf meinem PC (Windows 10, Chinesisch)
ich versuche, die folgende C++ Codes:
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;
}
Hmm ... Ich bin immer noch nicht zufrieden, also wende ich mich an boost::locale
(boost_1_65_1), aber es gibt einige seltsame Verhaltensweisen.
test2.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;
}
Problem 1: Die Codes in der Linkfehler führen, es sei denn, eine andere Header enthalten: #include <boost/filesystem.hpp>
, warum? Gibt es ein zu verwendendes Dokument, das boost::locale
enthält, das auch boost::filesystem
enthalten muss?
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)
Problem 2: Durch #include <boost/filesystem.hpp>
Zugabe läuft das Programm, aber encoding
ist "utf-8", warum? Ich dachte, es ‚cp936‘, im Einklang mit dem Ergebnis der test1.cpp
Frage zu sein: Was ist die beste Idee, die locale Codierung von C++ zu erhalten (zu demselben Ergebnis führt mit Pythons locale.getpreferredencoding()
). Danke vielmals!
Umwelt: win10 64bit, vs2015, boost_1_65_1 (Fenster Pre-Build, lib64-msvc-14,0)
Das Dokument zuerst lesen! – Silencer
@Silencer Wie können Sie sehen, dass ich das Dokument nicht gelesen habe?Sie könnten einen nützlicheren Kommentar geben, zum Beispiel den richtigen Platz des so genannten "Dokuments" – oz1
Es zeigt sich, weil http://www.boost.org/doc/libs/1_62_0/libs/locale/doc/html/examples .html zeigt genau, was Sie tun müssen. – UKMonkey