【问题标题】:Cannot open file with fopen(); function - C Programing [closed]无法使用 fopen() 打开文件;函数-C编程[关闭]
【发布时间】:2021-08-11 18:37:49
【问题描述】:
  1. 我正在尝试创建一个使用fopen(); 函数的程序。

  2. 我的问题是fopen(); 找不到文件。

    • 当我使用perror("Error"); 时,输出为Error: No such file or directory.
  3. 我已经阅读了这篇文章,但它们并没有解决我的问题:

  4. 我的操作系统是 ubuntu mate 20.04。

  5. 这是我的代码:

    /* File Name: firstTransaction.c 
     * File Mission: scan the input files and make the first transaction.
     */
    
    #include "defines.h"
    
    /*
     * first transaction file:
     * Allocating new memory for the assembly source file.
     * Scanning the assembly source file.
     * @param filename - the filename of the file that needs to be converted by the program.
     * @return output and files -
     *      output the assembly source file in a machine code.
     *      return extern, entry and object files.
     */
    
    /* function prototype */
    
    extern char * readText(FILE * ptr, long ic, long dc); /* the function defined on assistanceFunctions.c file */
    
    int firstTransaction(int * cf, char **av)
    {
        FILE * fp; /* a pointer for fopen function */
        char * filteredFile = NULL;
        long ic = 100, dc = 0; /* declaration and initialization of the instruction counter and current data counter */
        int i = 0, j = 0; /* indexes */
        int cfh = 0; /* compatible file holder */
        int fileNameLength = (int)strlen(av[i]); /* computing the first filename length to allocate memory */
        char * fileName = (char*) calloc(fileNameLength,sizeof (char)); /* allocating memory for the first filename */
        system("pwd");
        while(cf[i] != 0) /* while there is more compatible files to open */
        {
        cfh = cf[i]; /* cfh - compatible file holder, cf - compatible file array, set the next compatible file to cfh */
        strcpy(fileName,av[cfh]); /* copy the file name from the argv array */
        fp = fopen("fileName","r"); /* open the first compatible file for read */
        if(fp == NULL) /* if file does not exists */
        {
            perror("Error"); /* print the error - why the file not opened */
            i++; /* increment i by one and try to open the next filename */
        }else /* if the file was opened successfully, start scanning the file */
        {
            /*filteredFile = */ readText(fp, ic, dc); /* calling readText function with pointer to the start of the file that was opend by fopen function */
           /* while(filteredFile[j] != EOF)
                {
                    putchar(filteredFile[j]);
                    j++;
                }
            /* DONT FORGET TO CLOSE WITH FCLOSE(); */
        }
        }
        return 0;
    }
    
    
    

以下是其他文章中失败的测试解决方案的一些屏幕截图:

在 NeonFire 回答后编辑:

  1. 我的第一个代码使用了fp = fopen(fileName,"r");,但我的错误消息是通过这种方式手动生成的:printf("error, %s file does not exists\n", fileName); /* print error message to the user */
    • 这导致我没有找到真正的错误。
    • 然后我改为fp = fopen("fileName","r"); 并使用perror("Error"); 而不是第一种也是正确的方式。
    • 阅读 NeonFire 的答案后,我收到一个新错误错误:打开的文件太多。
    • 我从目录中删除了备份文件,但仍然有同样的错误。

在 Ted Lyngmo 评论后编辑: 我确实不得不关闭文件来解决 - “要打开多个文件的错误”。

【问题讨论】:

  • 您的fp = fopen("fileName","r"); 不正确。从 fileName 中删除引号,使其引用您的变量 char * fileName ,而不是字符串 "fileName"
  • @NeonFire 把它变成一个答案。会得到我的投票。
  • @TedLyngmo 简单的错误总能战胜我们!
  • 我认为 OP 很可能已经发现了根本原因,如果他们付出更多努力将此代码减少到最小的可重现示例。
  • 备份与此无关。你打开了很多文件,但你没有关闭它们……结果是“打开的文件太多”。 fclose 每个 FILE* 完成后。

标签: c linux


【解决方案1】:

您的fp = fopen("fileName","r"); 不正确。从 fileName 中删除引号,使其引用您的变量 char * fileName ,而不是字符串 "fileName" :)

【讨论】:

  • 您好,感谢您的回答!我首先尝试使用不带引号的fileName,但错误消息是我使用printf(); 函数发出的,这就是为什么我没有注意到我遇到的另一个错误,似乎是“错误:打开的文件太多”,我有一个包含我在父目录中的所有文件的备份目录,我现在尝试删除备份文件,也许它正在尝试递归读取。
  • @CrazyTux 这是一个不相关的问题,您在自己的代码中有提示 DONT FORGET TO CLOSE WITH FCLOSE(); (应该是 fclose(FILE*); 但足够接近) - 但是,这个答案回答了您关于无法打开文件。
猜你喜欢
  • 1970-01-01
  • 2010-12-17
  • 1970-01-01
  • 1970-01-01
  • 2015-01-04
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
相关资源
最近更新 更多