fopen没有在linux上工作,因为名字中包含德语字符的文件

fopen没有在linux上工作,因为名字中包含德语字符的文件,第1张

概述我在c中编写了一个代码,用于文件处理.文件名中包含一些德语字符.此代码在 Windows上完美运行.但它不适用于 Linux. fopen正在提供“无法打开文件”错误. 我检查了文件路径,文件存在那里.此外,我已阅读该文件夹的写入权限. 代码如下. #include <stdio.h>#include <stdlib.h>#include <string.h>int main(){ 我在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上,您可以用.替换您的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 - %s\n",strerror(errno));

(正如乔纳森提醒的那样,错误报告通常会发送给stderr)

然后再次运行您的程序.也许你有一个字符编码问题(在源文件或文件系统中).

你也可以在程序中使用strace(也许是ltrace)来理解它正在进行的实际系统调用.

总结

以上是内存溢出为你收集整理的fopen没有在linux上工作,因为名字中包含德语字符的文件全部内容,希望文章能够帮你解决fopen没有在linux上工作,因为名字中包含德语字符的文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1018552.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存