Mark DDone
Mark DDone

Reputation: 31

Wrong filename when using chinese characters

I'm trying to create a file on Windows using a Chinese character. The entire path is inside the variable "std::string originalPath", however, I have a charset problem that I simply cannot understand to overcome.

I have written the following code:

#include <iostream>
#include <boost/locale.hpp>
#include <boost/filesystem/fstream.hpp>
#include <windows.h>

int main( int argc, char *argv[] )
{

    // Start the rand
    srand( time( NULL ) );

    // Create and install global locale
    std::locale::global( boost::locale::generator().generate( "" ) );
    // Make boost.filesystem use it
    boost::filesystem::path::imbue( std::locale() );

    // Check if set to utf-8
    if( std::use_facet<boost::locale::info>( std::locale() ).encoding() != "utf-8" ){
        std::cerr << "Wrong encoding" << std::endl;
        return -1;
    }

    std::string originalPath = "C:/test/s/一.png";

    // Convert to wstring (**WRONG!**)
    std::wstring newPath( originalPath.begin(), originalPath.end() );

    LPWSTR lp=(LPWSTR )newPath.c_str();
    CreateFileW(lp,GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ |
            FILE_SHARE_WRITE,     NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL );

    return 0;

}

Running it, however, I get inside the folder "C:\test\s" a file of name "¦ᄌタ.png", instead of "一.png", which I want. The only way I found to overcome this is to exchange the lines

std::string originalPath = "C:/test/s/一.png";

// Convert to wstring (**WRONG!**)
std::wstring newPath( originalPath.begin(), originalPath.end() );

to simply

std::wstring newPath = L"C:/test/s/一.png";

In this case the file "一.png" appears perfectly inside the folder "C:\test\s". Nonetheless, I cannot do that because the software get its path from a std::string variable. I think the conversion from std::string to std::wstring is being performed the wrong way, however, as it can be seen, I'm having deep problem trying to understand this logic. I read and researched Google exhaustively, read many qualitative texts, but all my attempts seem to be useless. I tried the MultiByteToWideChar function and also boost::filesystem, but both for no help, I simply cannot get the right filename when written to the folder.

I'm still learning, so I'm very sorry if I'm making a dumb mistake. My IDE is Eclipse and it is set up to UTF-8.

Upvotes: 1

Views: 2498

Answers (2)

Mark DDone
Mark DDone

Reputation: 31

Thank you for your help, roeland. Finally I managed to find a solution and I simply used this following library: "http://utfcpp.sourceforge.net/". I used the function "utf8::utf8to16" to convert my original UTF-8 string to UTF-16, this way allowing Windows to display the Chinese characters correctly.

Upvotes: 1

roeland
roeland

Reputation: 5741

You need to actually convert the UTF-8 string to UTF-16. For that you have to look up how to use boost::locale::conv or (on Windows only) the MultiByteToWideChar function.

std::wstring newPath( originalPath.begin(), originalPath.end() ); won't work, it will simply copy all the bytes one by one and cast them to a wchar_t.

Upvotes: 2

Related Questions