[英]C - wchar_t prints unwanted characters
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. 下面的函数接受一个参数,比如C:\\
并列出其中的文件。 But not 100% correctly. 但不是100%正确。 For Turkish characters it prints some symbols, even though I used wchar_t
type. 对于土耳其语字符,它会打印一些符号,即使我使用了wchar_t
类型。
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 我目前正在使用带有CodeBlocks 16.01的Windows 7 x64
Strange part is, same function works perfectly fine under Ubuntu 16.04 with CodeBlocks. 奇怪的是, 在使用CodeBlocks的Ubuntu 16.04下 , 同样的功能完全正常 。
DIR *dir
is for ANSI, not Unicode. DIR *dir
用于ANSI,而不是Unicode。 Use _WDIR
instead. 请改用_WDIR
。
Windows has limited support for printing Unicode. Windows对打印Unicode的支持有限。 In MinGW use WriteConsoleW
for printing Unicode. 在MinGW中使用WriteConsoleW
来打印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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.