简体   繁体   English

如何停止从C中的文件读取

[英]how to stop reading from file in C

I am just trying to read each character of the file and print it out but when the file finishes reading, but I am getting a bunch of ? 我只是想读取文件的每个字符并将其打印出来,但是当文件完成阅读时,但我得到了一堆? after it finishes reading. 完成阅读后。 How do I fix it? 我如何解决它?

#include <stdio.h>

int main(void){
    FILE *fr;            /* declare the file pointer */

    fr = fopen ("some.txt", "r");  /* open the file for reading */
        /* elapsed.dta is the name of the file */
        /* "rt" means open the file for reading text */
    char c;
    while((c = getc(fr)) != NULL)
    {
        printf("%c", c);
    }
    fclose(fr);  /* close the file prior to exiting the routine */
    /*of main*/ 


    return 0;
}

In spite of its name, getc returns an int , not a char , so that it can represent all of the possible char values and, in addition, EOF (end of file). 尽管它的名称, getc返回一个int ,而不是一个char ,因此它可以表示所有可能的char值,此外还有EOF (文件末尾)。 If getc returned a char , there would be no way to indicate the end of file without using one of the values that could possibly be in the file. 如果getc返回了一个char ,那么在没有使用文件中可能存在的值之一的情况下,将无法指示文件的结尾。

So, to fix your code, you must first change the declaration char c; 因此,要修复代码,必须先更改声明char c; to int c; int c; so that it can hold the EOF marker when it is returned. 这样它可以在返回时保持EOF标记。 Then, you must also change the while loop condition to check for EOF instead of NULL . 然后,您还必须更改while循环条件以检查EOF而不是NULL

You could also call feof(fr) to test end of file separately from reading the character. 你也可以调用feof(fr)来单独测试文件结尾来读取字符。 If you did that, you could leave c as a char , but you would have to call feof() after you read the character but before you printed it out, and use a break to get out of the loop. 如果你这样做了,你可以把c feof() char ,但是你必须在读完字符之后但是在打印出来之前调用feof() ,并使用break来摆脱循环。

If unsuccessful, fgetc() returns EOF. 如果不成功, fgetc()将返回EOF。

int c;
while ((c = getc(fr)) != EOF) 
{ 
    printf("%c", c); 
}

fgetc()在文件结尾返回EOF ,而不是NULL

将“NULL”替换为“EOF”。

Change this 改变这个

char c;
while((c = getc(fr)) != NULL)
{
    printf("%c", c);
}

to

char c;
int charAsInt;
while((charAsInt = getc(fr)) != EOF)
{
     c = (char) charAsInt;
     printf("%c", c);
}

In other words: You need to compare against EOF , not NULL . 换句话说:您需要与EOF进行比较,而不是NULL You also need to use an int variable to receive the return value from fgetc . 您还需要使用int变量来从fgetc接收返回值。 If you use a char , the comparison with EOF may fail, and you'll be back where you started. 如果使用char ,则与EOF的比较可能会失败,并且您将返回到开始的位置。

Others have already addressed the issue you're having, but rather than using printf("%c", c); 其他人已经解决了你所遇到的问题,而不是使用printf("%c", c); it is probably much more efficient to use putchar(c); 使用putchar(c);可能效率更高putchar(c); . There is quite a bit of overhead involved when you ask printf to print just one character. 当你要求printf打印一个字符时,会涉及相当多的开销。

getc returns an int . getc返回一个int

change char c , to int c . char c更改为int c

also getc returns EOF , change your test against NULL to a test against EOF getc也会返回EOF ,将针对NULL的测试更改为针对EOF的测试

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

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