Convert UTF-8 to wide char

#ifndef UNICODE
#define UNICODE
#endif

#include <Windows.h>
#include <cstdio>
#include <fstream>

using namespace std;

int main()
{
    FILE* resFile;
    char multiByteStr[256];
    ifstream oFile;
    FILE* exampleFile;
    TCHAR buffer[256];      
    system("chcp 65001");

    resFile = _wfopen(L"foo",L"w, ccs=UTF-8");             
    fwprintf(resFile,L"%s",L"C:\\exsistingFolder\\zażółć gęśłą jaźń ☺☻♥♦• ć.txt");    
    fclose(resFile);   

    oFile.open(L"foo");    
    oFile.getline(multiByteStr,256,'\n');       
    oFile.close();    

    MultiByteToWideChar(CP_UTF8,0,multiByteStr,256,buffer,256);    
    wprintf(L"%s",buffer);

    exampleFile = _wfopen(buffer,L"w, ccs=UTF-16LE");       
    fwprintf(exampleFile,L"%s",buffer);     
    fclose(exampleFile);

    system("pause");
    return 0;
}

As you can see, the program should create a file "foo" resFilethat contains the full path to the file that will be created, and this new file exampleFileshould contain the path to itself. Although autos gives the buffer that has the correct line during debugging in Visual Studio 2010, an exampleFile is not created. Why?
And one more wprintfthing : why doesn't it display extended characters, although I switched the console font to Lucida Console - one that can deal with unsigned characters.

Ps. exampleFile indicates NULL, even after _wfopen, and the last character of the buffer is '/0'.

+1
3

: _wfopen , UTF-8 BOM, MultiByteToWideChar , .

+1

. , MultiByteToWideChar() , multiByteStr, , . , . MultiByteToWideChar(), , , . . , .

:

#define UNICODE

#include <Windows.h>
#include <cstdio>
#include <fstream>

using namespace std;

void pause()
{
    wcin.ignore();
    wcin.get();
}

int main()
{
    FILE* resFile;
    char multiByteStr[256] = {0};
    ifstream oFile;
    FILE* exampleFile;
    WCHAR buffer[256] = {0};

    SetConsoleOutputCP(CP_UTF8);

    resFile = _wfopen(L"foo",L"w, ccs=UTF-8");
    if (!resFile) {
        wprintf(L"Unable to create foo");
        goto done;
    }

    fwprintf(resFile,L"%s",L"C:\\exsistingFolder\\zażółć gęśłą jaźń ☺☻♥♦• ć.txt");
    fclose(resFile);

    if (!oFile.open(L"foo")) {
        wprintf(L"Unable to open foo");
        goto done;
    }

    oFile.getline(multiByteStr,255,'\n');
    oFile.close();

    if (MultiByteToWideChar(CP_UTF8,0,multiByteStr,-1,buffer,256) == 0) {
        wprintf(L"Unable to convert UTF-8 to UTF-16. Error: %u", GetLastError());
        goto done;
    }

    exampleFile = _wfopen(buffer,L"w, ccs=UTF-16LE"); 
    if (!exampleFile) {
        wprintf(L"Unable to create file: %s", buffer);
        goto done;
    }

    fwprintf(exampleFile,L"%s",buffer); 
    fclose(exampleFile);

    wprintf(L"created file: %s", buffer);

done:
    pause();
    return 0;
}
+2

/? (Windows) C-, :

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE* pFile;
    wchar_t buffer[256];

    _wfopen_s(&pFile,L"foo",L"w, ccs=UTF-8");
    fputws(L"C:/existingFolder/zażółć gęśłą jaźń ☺☻♥♦• ć.txt",pFile);
    fclose(pFile);

    _wfopen_s(&pFile,L"foo",L"r, ccs=UTF-8");
    fgetws(buffer,_countof(buffer),pFile);
    fclose(pFile);

    _wfopen_s(&pFile,buffer,L"w, ccs=UTF-16LE");
    fputws(buffer,pFile);
    fclose(pFile);

    return 0;
}
+1

All Articles