C - wchar_t 打印不需要的字符
C - wchar_t prints unwanted characters
哎。
我正在尝试列出一个包含非英文字符文件的文件夹。
下面的函数接受一个参数,比如 C:\
并列出其中的文件。但不是 100% 正确。对于土耳其字符,它会打印一些符号,即使我使用 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);
}
我目前正在使用 Windows 7 x64 和 CodeBlocks 16.01
奇怪的是,相同的功能在 Ubuntu 16.04 和 CodeBlocks 下工作得很好。
DIR *dir
适用于 ANSI,而非 Unicode。请改用 _WDIR
。
Windows 对打印 Unicode 的支持有限。在 MinGW 中使用 WriteConsoleW
打印 Unicode。示例:
#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);
}
哎。
我正在尝试列出一个包含非英文字符文件的文件夹。
下面的函数接受一个参数,比如 C:\
并列出其中的文件。但不是 100% 正确。对于土耳其字符,它会打印一些符号,即使我使用 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);
}
我目前正在使用 Windows 7 x64 和 CodeBlocks 16.01
奇怪的是,相同的功能在 Ubuntu 16.04 和 CodeBlocks 下工作得很好。
DIR *dir
适用于 ANSI,而非 Unicode。请改用 _WDIR
。
Windows 对打印 Unicode 的支持有限。在 MinGW 中使用 WriteConsoleW
打印 Unicode。示例:
#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);
}