Search code examples
c++stringwstring

Convert between wstring and string , got different results with "same" way


I use a function s2ws() (search from the SO,if you find something wrong please let me know)convert from string to wstring,then I use tinyxml2 to read something from xml.As we all know ,some of tinyxml2 interface use char * as input so does the return value.

The reason why convert from string to wstring is the project all using wchar_t types to deal with string.

/*
    string converts to wstring
*/
std::wstring s2ws(const std::string& src)     
{  
    std::wstring res = L"";
    size_t const wcs_len = mbstowcs(NULL, src.c_str(), 0);
    std::vector<wchar_t> buffer(wcs_len + 1);
    mbstowcs(&buffer[0], src.c_str(), src.size());
    res.assign(buffer.begin(), buffer.end() - 1);

    return res;
}  


/*
    wstring converts to string
*/
std::string ws2s(const std::wstring & src)
{ 
   setlocale(LC_CTYPE, "");

   std::string res = "";

   size_t const mbs_len = wcstombs(NULL, src.c_str(), 0);

   std::vector<char> buffer(mbs_len + 1);

   wcstombs(&buffer[0], src.c_str(), buffer.size());

   res.assign(buffer.begin(), buffer.end() - 1);

   return res;
}

The ClassES-Attribute will return char *,funciton s2ws will convert string to wstring. These two ways got different result in map m_UpdateClassification. The second method is between #if 0 and #endif. But I thinks these two ways should make no difference.

The second method will got empty string after convert,can not figure out why,If you have any clue,please let me know.

    typedef std::map<std::wstring, std::wstring> CMapString;
    CMapString   m_UpdateClassification;

    const wchar_t * First = NULL;
    const wchar_t * Second = NULL;

    const char *name = ClassES->Attribute( "name" );

    const char *value = ClassES->Attribute( "value" );

    std::wstring wname = s2ws(name);

    std::wcout<< wname << std::endl;

    First = wname.c_str();

    std::wstring wvalue = s2ws(value);

    std::wcout<< wvalue << std::endl;

    Second = wvalue.c_str();

#if 0
    First = s2ws(ClassES->Attribute( "name" )).c_str();
    if( !First ) { m_ProdectFamily.clear(); return FALSE; }

    Second = s2ws(ClassES->Attribute( "value" )).c_str();
    if( !Second ) { m_ProdectFamily.clear(); return FALSE; }
#endif  

    m_UpdateClassification[Second] = First;

Solution

  • I think I found the reason,I assgin wchar_t * to wstring,After modfiy code like this,everything run well.

        std::wstring First = L"";
    
        std::wstring Second = L"";
    
        First = s2ws(ClassES->Attribute("name"));
        if( First.empty() ) { m_ProdectFamily.clear(); return FALSE; }
    
        Second = s2ws(ClassES->Attribute("value"));
        if( Second.empty() ) { m_ProdectFamily.clear(); return FALSE; } 
    

    Another question,Should I check the result of s2ws(mbstowcs) ws2s(wcstombs)?