无法使用fopen()打开文件;函数-C编程

  1. 我正在尝试创建一个使用该fopen();函数的程序。

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

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

    • fopen() 返回空指针,但文件肯定存在
    • 无法使用 fopen() 打开文件
    • 这意味着我已经尝试使用fopen();文件的 exec 路径,并且我已经检查过文件名是我要查找的特定文件名。
    • 我也曾经exec("pwd");看到我是正确的目录。
  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 existsn", fileName); /* print error message to the user */
    • 这导致我没有发现我真正的错误。
    • 然后我改为fp = fopen("fileName","r");并使用perror("Error");而不是第一种和正确的方式。
    • 在我阅读 NeonFire 答案后,我收到了一个新错误错误:打开的文件太多。
    • 我从目录中删除了备份文件,但仍然出现相同的错误。

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

回答

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

  • @CrazyTux 这是一个无关的问题,你在你自己的代码中有一个提示`不要忘记用 FCLOSE() 关闭;`(它应该是 `fclose(FILE*);` 但足够接近) - 但是,这个答案回答了你的问题关于无法打开文件。

以上是无法使用fopen()打开文件;函数-C编程的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>