fopen没有在linux上工作,因为名字中包含德语字符的文件
发布时间:2020-12-13 23:03:51 所属栏目:Linux 来源:网络整理
导读:我在c中编写了一个代码,用于文件处理.文件名中包含一些德语字符.此代码在 Windows上完美运行.但它不适用于 Linux. fopen正在提供“无法打开文件”错误. 我检查了文件路径,文件存在那里.此外,我已阅读该文件夹的写入权限. 代码如下. #include stdio.h#include
我在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,"%sn",errorMessage); } return 1; } 有什么输入吗? 解决方法
在Linux上,您可以用.替换您的sprintf调用
snprintf (errorMessage,sizeof(errorMessage),"Could not open file %s - %m",fileName); (一般提示是为了避免sprintf,因为可能存在缓冲区溢出并且只使用snprintf) 如果要避免使用GLibc特定的%m格式说明符,并使用更多标准函数代码 snprintf (errorMessage,"Could not open file %s - %s",fileName,strerror(errno)); 并且不要忘记#include< errno.h>,并仔细阅读errno(3)手册页. 顺便说一句,你可以避免同时使用snprintf和printf以及代码 fprintf (stderr,"Cannot open file %s - %sn",strerror(errno)); (正如乔纳森提醒的那样,错误报告通常会发送给stderr) 然后再次运行您的程序.也许你有一个字符编码问题(在源文件或文件系统中). 你也可以在程序中使用strace(也许是ltrace)来理解它正在进行的实际系统调用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容