【问题标题】:Wrong File String for fopen?fopen 的文件字符串错误?
【发布时间】:2013-08-05 05:18:46
【问题描述】:

我的桌面上有一个名为 fun 的文本文件,但是当我通过时:

FILE* fp;

if((fp = fopen("/Users/<username>/Desktop/fun", "r")) == NULL)
{
    printf("File didn't open\n");
    exit(1);
}

fp 为空。我也试过了

/home/<username>/Desktop/fun

还有许多变体,但我似乎仍然无法获得正确的文件路径。我是使用文件和 C 的新手。任何帮助将不胜感激。

【问题讨论】:

  • 你能给我们看看代码吗?
  • FIlE* 应该是FILE*

标签: c file file-io


【解决方案1】:

fopen() 不能扩展 shell 关键字。

改变

FILE* fp = fopen("~/Desktop/fun.txt", "r")

FILE* fp = fopen("/home/<yourusername>/Desktop/fun.txt", "r")

'~', '*' 这样的字符由shell 解释并展开。

【讨论】:

  • 所以我得到了下一行: FILE* fp = fopen("/home//Desktop/input.txt", "r") 但它没有打开它。我做错了什么。但如果我将其更改为 freopen(/Users//Desktop/input.txt", "r", stdin) 它运行良好
  • @wm.p1us,你得到了什么错误?尝试使用perror() 出错。
【解决方案2】:

您不能在路径名中使用~ 来表示用户的主目录。 shell 和其他一些应用程序可以识别该符号,但它不是 Unix 文件系统接口的一部分。您需要拼出用户的实际主目录。

fopen("/home/username/Desktop/fun.txt", "r")

【讨论】:

    【解决方案3】:

    路径中的~ 可能是问题所在。是你的 shell 在命令行上扩展它。 fopen 不会调用 shell 来对路径进行替换,您需要自己进行。

    因此,将完整(相对或绝对)路径传递给 fopen,而不是需要 shell 扩展(~、通配模式或 shell 变量)的东西。

    【讨论】:

      【解决方案4】:

      您需要扩展~。使用getenv("HOME")

      getenvopengroup 甚至提供了一些代码:

      const char *name = "HOME";
      char *value;
      value = getenv(name);
      

      【讨论】:

      • 我看到了您指向POSIX.1-2004 的链接,并为您提供了指向POSIX.1-2008 的链接。 The getenv() function。我还赞成你提供了任何指向文档的链接。
      • 但是还要注意value应该是const,因为明确禁止修改返回的字符串。
      【解决方案5】:
      /*===exphome===([o]i)==================================================
       * if SIn is not NULL then
       *    if SIn starts with '~'
       *    then expands $HOME, prepends it to the rest of SIn, and
       *         stores result in SOut or, if SOut==NULL, in a new
       *         allocated string and returns it
       *    else if SOut!=NULL
       *         then copies SIn into SOut and returns SOut
       *         else returns duplicated SIn
       * else returns NULL
      =*===================================================================*/
          char *exphome(char *SOut, char *SIn)
          {char *Rt= NULL;
           char *envhome= NULL;
      
           if(SIn)
              if(*SIn=='~' && (envhome=getenv("HOME")))
                {Rt= malloc(strlen(SIn)+strlen(envhome)+1);
                 strcpy(Rt, envhome); strcat(Rt, SIn+1);
                 if(SOut) {strcpy(SOut, Rt); free(Rt); Rt= SOut;}
                }
              else if(SOut) {strcpy(SOut, SIn); Rt= SOut;}
              else Rt= strdup(SIn);
      
           return Rt;
          } /*exphome*/
      

      然后

      fopen(exphome(NULL, yourfile), ...)
      

      【讨论】:

        【解决方案6】:

        看起来答案已提示对原始问题进行编辑。但是,正如目前所写的那样,文件名上没有扩展名?这是真的吗?还是文件以“*.txt”等结尾?

        【讨论】:

          【解决方案7】:

          仔细检查您是否拥有正确的完整文件路径。转到文件,右键单击它并选择“属性”。您输入的路径是否完全如图所示,包括任何后缀? IE。如果文件名为“file.txt”,请确保在代码中包含“.txt”后缀。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-10-09
            • 1970-01-01
            • 1970-01-01
            • 2017-08-06
            • 2020-05-22
            • 1970-01-01
            • 1970-01-01
            • 2021-09-08
            相关资源
            最近更新 更多