1

TinyXML2 を使用して、ディスクから xml ドキュメントをロードしています。

ファイルのパス (configFileName) はwstringであり、次のように文字列に変換しています。

    tinyxml2::XMLDocument doc;
    std::string fileName(configFileName.begin(), configFileName.end());
    doc.LoadFile(fileName.c_str());

これは機能しますが、私のプログラムが中国語や韓国語などの 2 バイト OS で実行され、上記の wstring から string への変換で文字が失われる場合があります。

次のようなパスをロードするにはどうすればよいですか。

wstring wPathChinese = L"C:\\我明天要去上海\\在这里等我\\MyProgram";

編集

文字列を変換するために次のことを試みましたが、それでも漢字が壊れます。

std::string ConvertWideStringToString(std::wstring source)
{
    //Represents a locale facet that converts between wide characters encoded as UCS-2 or UCS-4, and a byte stream encoded as UTF-8.
    typedef std::codecvt_utf8<wchar_t> convert_type;
    // wide to UTF-8
    std::wstring_convert<convert_type, wchar_t> converter;
    std::string converted_str = converter.to_bytes(source);

    return converted_str;
}

string pathCh2 = ConvertWideStringToString(wPathChinese);
4

1 に答える 1

4

を使用することをお勧めします

XMLError tinyxml2::XMLDocument::LoadFile( FILE *)   

したがって、次のようなことを簡単に行うことができます。

FILE* fp = nullptr;
errno_t err = _wfopen_s(&fp, configFileName.c_str(), L"rb" );
if( fp && !err )
{
    tinyxml2::XMLDocument doc;
    doc.LoadFile( fp );
    fclose( fp );
}

そして、wstring と string の間の悪夢のような変換を忘れてしまいます...

于 2015-05-13T14:23:47.573 に答える