如何停止从C中读取文件

发布于 2024-08-18 15:37:18 字数 542 浏览 7 评论 0原文

我只是想读取文件的每个字符并将其打印出来,但是当文件完成读取时,但我得到了一堆?读完后。我该如何修复它?

#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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

注定孤独终老 2024-08-25 15:37:18

不管它的名字如何,getc 返回一个 int,而不是 char,因此它可以表示所有可能的 char 值,并且,另外,EOF(文件结束)。如果 getc 返回一个 char,那么如果不使用文件中可能存在的值之一,就无法指示文件结尾。

因此,要修复您的代码,您必须首先将声明 char c; 更改为 int c;,以便它在返回时可以保存 EOF 标记。然后,您还必须更改 while 循环条件以检查 EOF 而不是 NULL

您还可以调用 feof(fr) 来测试文件结尾与读取字符分开。如果这样做,您可以将 c 保留为 char,但您必须在读取字符之后但之前调用 feof()您将其打印出来,并使用 break 退出循环。

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). 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.

So, to fix your code, you must first change the declaration char c; to int c; so that it can hold the EOF marker when it is returned. Then, you must also change the while loop condition to check for EOF instead of NULL.

You could also call feof(fr) to test end of file separately from reading the character. 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.

染年凉城似染瑾 2024-08-25 15:37:18

如果不成功,fgetc() 返回 EOF。

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

If unsuccessful, fgetc() returns EOF.

int c;
while ((c = getc(fr)) != EOF) 
{ 
    printf("%c", c); 
}
一口甜 2024-08-25 15:37:18

将其更改

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

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

换句话说:您需要与 EOF 进行比较,而不是与 NULL 进行比较。您还需要使用 int 变量来接收 fgetc 的返回值。如果您使用 char,与 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. You also need to use an int variable to receive the return value from fgetc. If you use a char, the comparison with EOF may fail, and you'll be back where you started.

绳情 2024-08-25 15:37:18

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

fgetc() returns EOF on end-of-file, not NULL.

孤星 2024-08-25 15:37:18

将“NULL”替换为“EOF”。

Replace "NULL" with "EOF".

累赘 2024-08-25 15:37:18

其他人已经解决了您遇到的问题,但使用 putchar(c); 可能比使用 printf("%c", c); 更有效。代码>.当您要求 printf 仅打印一个字符时,会涉及相当多的开销。

Others have already addressed the issue you're having, but rather than using printf("%c", c); it is probably much more efficient to use putchar(c);. There is quite a bit of overhead involved when you ask printf to print just one character.

套路撩心 2024-08-25 15:37:18

getc 返回一个int

char c 更改为 int c

同时getc返回EOF
将针对 NULL 的测试更改为针对 EOF 的测试

getc returns an int.

change char c, to int c.

also getc returns EOF,
change your test against NULL to a test against EOF

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文