【发布时间】:2012-10-09 22:31:23
【问题描述】:
我用 c 编写了一个代码,用于文件处理。文件名中包含一些德语字符。此代码在 Windows 上运行良好。但它在 Linux 中不起作用。 fopen 给出“无法打开文件”错误。 我检查了文件路径,文件存在那里。另外,我对该文件夹有读写权限。
代码如下。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
const char *fileName = "/users/common/haëlMünchen.txt";
FILE * pFile;
char errorMessage[256];
pFile = fopen (fileName,"r");
if (pFile != NULL)
{
fprintf (stdout,"fopen example",errorMessage);
fclose (pFile);
}
else
{
sprintf(errorMessage, "Could not open file %s", fileName);
fprintf(stdout, "%s\n", errorMessage);
}
return 1;
}
对此有何意见?
【问题讨论】:
-
您的文件和文件系统路径的编码是什么? Linux 只将路径视为一堆字节。我怀疑编码不同,这就是您无法打开它的原因。由于字节不完全匹配,因此它不是同一个文件。您需要正确编码。
-
可能是 Unicode (UTF-8) 与 CP1252(显示的字符类似于 ISO 8859-1 或 8859-15)。
-
顺便说一句:记事本是否保留字符 ë 的外观?我将赌注押在字符集编码上(一些系统假定它是 utf-8,因为扩展了 ascii 字符......)
-
使用
ls h*chen.txt | od -t x1查看文件系统上文件名的十六进制字节。在您的程序中以十六进制打印出fileName中的字节。寻找不同之处。