简体   繁体   English

fclose()始终返回EOF

[英]fclose() always returns EOF

I have a function that reads integers with certain format from a file. 我有一个函数,可以从文件中读取具有特定格式的整数。 It works fine as desired, but whenever I tried to close the file with fclose(), fclose() always returns EOF. 它可以按需正常运行,但是每当我尝试使用fclose()关闭文件时,fclose()始终会返回EOF。

Any suggestions why? 有什么建议吗? I am a student and still learning. 我是学生,还在学习。 I have put the code below. 我把代码放在下面。 Please let me know if you need the "processing" code. 请让我知道是否需要“处理”代码。 Thanks :) 谢谢 :)

// Open the file
FILE *myFile = fopen(fileName, "r");
if(myFile == NULL){
    //Handle error
    fprintf(stderr, "Error opening file for read \n");
    exit(1);
}

while(myFile != EOF)
{
     // read and process the file
     // this part works.
}

// always returns EOF here. WHY?
if (fclose(myFile) == EOF) {
    // Handle the error!
    fprintf(stderr, "Error closing input file.\n");
    exit(1);
}
printf("Done reading the file.");

EDIT: Thank you for all the replies. 编辑:感谢您的所有答复。 Sorry I cannot post the code as this is part of my homework. 抱歉,我无法发布代码,因为这是我的家庭作业。 I was trying to get some help, I am not asking you guys to make the code for me. 我试图获得一些帮助,但并不是要你们为我编写代码。 Posting code is illegal according to my Prof (since other students can see and probably copy, that's what he told me). 根据我的教授说,张贴代码是非法的(因为其他学生可以看到并且可能复制,所以他告诉了我)。 I can only post the code after Sunday. 我只能在星期日之后发布代码。 For now, I will try to modify my code and avoid using fscanf. 现在,我将尝试修改代码并避免使用fscanf。 Thanks and my apology. 谢谢,我道歉。

Since there is nothing that you can do to a regular file open in read-only-mode that would cause a fclose to error out, you very probably have a bug in the code you didn't show which is stomping on the myFile structure. 由于您无法对以只读模式打开的常规文件执行任何操作,否则将导致fclose出错,因此您很可能在未显示的代码中存在一个错误,这严重影响了myFile结构。

Also the test myFile != EOF will never be true because you set it to the return of fopen which will never give you EOF and you already checked it for NULL Did you mean something like: 同样,测试myFile != EOF永远不会成立,因为您将其设置为fopen的返回值,这将永远不会给您EOF并且您已经对其进行了NULL检查。

while((c = fgetc(myFile)) != EOF) {
    // do stuff
}

This: 这个:

while(myFile != EOF)

is actually illegal (a constraint violation). 实际上是非法的(违反约束)。 Any conforming C compiler is required to issue a diagnostic; 任何符合要求的C编译器都需要发出诊断; gcc, by default, merely issues a warning (which does qualify as a "diagnostic"). 默认情况下,gcc仅发出警告(确实具有“诊断性”)。

If gcc gave you a warning, you should pay attention to it; 如果gcc给您警告,则应予以注意; gcc often issues warnings for things that IMHO should be treated as fatal errors. gcc经常针对应将IMHO视为致命错误的事项发出警告。 And if it didn't give you a warning, you're probably invoking it with options that disable warnings (which is odd, because it does produce that warning by default). 而且,如果它没有向您发出警告,则可能是通过禁用警告的选项来调用它(这很奇怪,因为默认情况下它确实会产生警告)。 A good set of options to use is -Wall -Wextra -std=c99 -pedantic (or adjust the -std=... option to enforce a different version of the standard if you like). 可以使用-Wall -Wextra -std=c99 -pedantic作为一组很好的选项(或者,如果愿意,可以调整-std=...选项以强制执行其他版本的标准)。

myFile is of pointer type, specifically FILE* . myFile是指针类型,特别是FILE* EOF is of type int , and typically expands to (-1) . EOFint类型,通常扩展为(-1) You cannot legally compare a pointer value to an integer value (except for the special case of a null pointer constant, but that doesn't apply here.) 您不能合法地将指针值与整数值进行比较(空指针常量的特殊情况除外,但这不适用于此处。)

Assuming the program isn't rejected, I'd expect that to result in an infinite loop, since myFile would almost certainly never be equal to EOF . 假设程序没有被拒绝,我希望这会导致无限循环,因为myFile几乎肯定不会等于EOF

You could change it to 可以将其更改为

while (!feof(myFile))

but that can cause other problems. 但这可能会导致其他问题。 The correct way to detect end-of-file while reading from a file is to use the value returned by whatever function you're using read the data. 从文件读取时检测文件结尾的正确方法是使用所使用的任何函数返回的值读取数据。 Consult the documentation for the function you're using to see what it returns when it encounters end-of-file or an error condition. 有关正在使用的功能,请查阅文档,以查看遇到文件结尾或错误情况时返回的内容。 The feof() function is useful for determining, after you've finished reading, whether you encountered end-of-file or an error condition. feof()函数可用于确定,你读完之后 ,你是否遇到档案结尾或错误条件。

What the errno said? errno说了什么? add this to your code: 将此添加到您的代码:

#include <errno.h>
#include <string.h>

if (fclose(myFile) == EOF) {
    // Handle the error!
    fprintf(stderr, "Error closing input file. and errno = %d, and error = %s\n", errno, strerror(errno));
    exit(1);
}

Hope this help. 希望能有所帮助。

Regards. 问候。

while(myFile != EOF)
 {
 // read and process the file
 // this part works.
 }

If this part is ok then 如果这部分没问题

fclose(myFile);

definitely return you EOF.Because the while loop terminates only myFile == EOF(this is a wrong comparison, do not ignore warnings).comparision between pointer and int.As EOF is a macro defined in stdio.h file And according to glibc it -1.Your loop terminates that means your myFile pointer changed to EOF some whire.This is your mistake. 肯定会返回EOF。因为while循环仅终止myFile == EOF(这是一个错误的比较,请不要忽略警告)。指针与int之间的比较。由于EOF是stdio.h文件中定义的宏并且根据glibc -1。您的循环终止,这意味着您的myFile指针更改为EOF,这是您的错误。

just go through your code you must be change the FILE pointer myFile which should not be over written by you As it point to a file structure which is used for all file operation. 只需检查您的代码,您就必须更改FILE指针myFile,该指针不应被您覆盖。因为它指向用于所有文件操作的文件结构。

NOTE myFile which is a pointer to a file should not be used as lvalue in any assignment statement. 注意 myFile是指向文件的指针,在任何赋值语句中都不应将其用作左值。

Change while(myFile != EOF){...} by while(!feof(myFile)){...} 通过while(!feof(myFile)){...}更改while(myFile != EOF){...} while(!feof(myFile)){...}
myFile is a pointer to a FILE struct (a memory address). myFile是指向FILE结构(内存地址)的指针。 Not a "end of file" indicator. 不是“文件结束”指示符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM