简体   繁体   English

使用C代码在Windows上使用波兰语字符读取文件名

[英]Reading file names with polish characters on windows using C code

I must note that developing code for windows platform is a mystery for me at the moment. 我必须指出,目前对Windows平台开发代码对我来说还是个谜。

I have an issue with going through a directory and reading up files, where polish characters are present. 我在遍历目录并读取其中存在波兰语字符的文件时遇到问题。 Following code snippets work on windows in english and polish version, but not in german. 以下代码段在英语和波兰语版本的Windows上均有效,但在德语中则无效。 What's the reason? 什么原因? I've tried with POSIX opendir() / readdir() and with winapi FindFirstFile() / FindNextFile() functions. 我已经尝试过使用POSIX opendir() / readdir()和winapi FindFirstFile() / FindNextFile()函数。 They gave same results. 他们给出了相同的结果。
Consider following program. 考虑以下程序。 It reads up files ( in POSIX and winapi way ) within test directory and prints them out. 它读取测试目录中的文件(以POSIX和winapi方式)并将其打印出来。

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

int main(void)
{
    char* name;
    name = _getcwd(NULL, 0);

    char* tmp = realloc(name, strlen(name)*2);
    if (tmp != NULL) {
        name = tmp;
    }
    else {
        perror("realloc");
        free(name);
        exit(1);
    }

    strcat(name, "\\test");
    DIR* dir = opendir(name);
    struct dirent* s;
    while ((s = readdir(dir)) != NULL) {
        printf("%s\n", s->d_name);
    }
    closedir(dir);
    free(s);

    strcat(name, "\\*");
    HANDLE dir_w;
    WIN32_FIND_DATA s_w;
    if ((dir_w = FindFirstFile(name, &s_w)) != INVALID_HANDLE_VALUE) {
        while (FindNextFile(dir_w, &s_w) != 0) {
            printf("%s\n", s_w.cFileName);
        }
    }
    FindClose(dir_w);

    free(name);

    return 0;
}

ISSUE : on windows with german language polish characters are interpreted as ASCII characters. 问题 :在具有德语波兰语字符的窗口上,将其解释为ASCII字符。 If, for example, file has name: 例如,如果文件具有名称:

ąęćś.txt ąęćś.txt

then on english language windows it is read up correctly, but on german one is read as 然后在英语语言窗口中可以正确读取,而在德语窗口中则读取为

aecs.txt aecs.txt

but such file does not exist. 但是该文件不存在。 Should I go with wchar_t type and functions specific to it (FindNextFilew) ? 我应该使用wchar_t类型和特定于它的函数(FindNextFilew)吗? I don't have german windows so it is hard for me to reproduce the error. 我没有德语窗口,因此很难重现该错误。 I've been trying to change code page with command chcp 1250 but that didn't help. 我一直在尝试使用命令chcp 1250更改代码页,但这无济于事。 Setting locale to pl also did not help. 将语言环境设置为pl也无济于事。

Compiler: gcc 5.0.0 编译器:gcc 5.0.0

The C standard library functions you call will be using ANSI encoded text which as I'm sure you know cannot handle international text. 您调用的C标准库函数将使用ANSI编码的文本,据我所知,它不能处理国际文本。

So you'll want to use the native Unicode Windows APIs rather than the legacy ANSI versions. 因此,您将要使用本机Unicode Windows API,而不是旧版ANSI版本。 That means wchar_t based functions with W prefixes like FindFirstFileW . 这意味着带有W前缀的基于wchar_t的函数,例如FindFirstFileW For a start you should enable the Unicode conditionals so that the FindFirstFile macro expands to FindFirstFileW . 首先,您应该启用Unicode条件,以便FindFirstFile宏扩展为FindFirstFileW

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM