【发布时间】:2014-10-25 17:07:19
【问题描述】:
我必须指出,目前为 Windows 平台开发代码对我来说是一个谜。
我在浏览目录和读取文件时遇到问题,其中存在波兰语字符。以下代码 sn-ps 在英语和波兰语版本的 Windows 上工作,但不是德语。什么原因?我已经尝试过使用 POSIX opendir() / readdir() 和 winapi FindFirstFile() / FindNextFile() 函数。他们给出了相同的结果。
考虑以下程序。它读取测试目录中的文件(以 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 :在带有德语波兰语字符的 Windows 上被解释为 ASCII 字符。例如,如果文件具有名称:
ąęćś.txt
然后在英语窗口上它被正确读取,但在德语窗口上被读取为
aecs.txt
但是这样的文件不存在。我应该使用wchar_t 类型和特定于它的功能(FindNextFilew)吗?我没有德语窗口,所以我很难重现错误。我一直在尝试使用命令chcp 1250 更改代码页,但这并没有帮助。将语言环境设置为 pl 也无济于事。
编译器:gcc 5.0.0
【问题讨论】:
-
也许尝试为 Unicode 编译?