1

Hey.

I am trying to list a folder which has non-english character files. Below function takes an argument, say C:\ and lists the files inside it. But not 100% correctly. For Turkish characters it prints some symbols, even though I used wchar_t type.

void listFolder(const wchar_t* path){
DIR *dir;
struct _wdirent *dp;
wchar_t * file_name;
wchar_t  fullpath[MAX_PATH];
dir = _wopendir(path);

while ((dp=_wreaddir(dir)) != NULL) {
    //printf("[i]debug: \t%s\n", dp->d_name);
    if ( !wcscmp(dp->d_name, L".") || !wcscmp(dp->d_name, L"..") ){
        // do nothing
    } 
    else {
        file_name = dp->d_name; // use it
        wprintf(L"[*]file_name: \t\"%ls\"\n",file_name);
    }
}
_wclosedir(dir);

}

I am currently using Windows 7 x64 with CodeBlocks 16.01

Strange part is, same function works perfectly fine under Ubuntu 16.04 with CodeBlocks.

mert yeniay
  • 39
  • 1
  • 6
  • This is normal behaviour for the windows console, see http://stackoverflow.com/questions/15827607/writeconsolew-wprintf-and-unicode You need to change the codepage to one that supports the characters you want to print – Richard Nov 22 '16 at 12:42

1 Answers1

0

DIR *dir is for ANSI, not Unicode. Use _WDIR instead.

Windows has limited support for printing Unicode. In MinGW use WriteConsoleW for printing Unicode. Example:

#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <windows.h>

void myprint(const wchar_t* str)
{
    WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wcslen(str), NULL, NULL);
}

void listFolder(const wchar_t* path)
{
    _WDIR *dir = _wopendir(path);
    if (!dir) return;

    struct _wdirent *dp;
    while ((dp=_wreaddir(dir)) != NULL)
    {
        if ( wcscmp(dp->d_name, L".") == 0 || wcscmp(dp->d_name, L"..") == 0)
            continue;
        myprint(dp->d_name);
        wprintf(L"\n");
    }
    _wclosedir(dir);
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77