如何停止从C中读取文件
我只是想读取文件的每个字符并将其打印出来,但是当文件完成读取时,但我得到了一堆?读完后。我该如何修复它?
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不管它的名字如何,
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 anint
, not achar
, so that it can represent all of the possible char values and, in addition,EOF
(end of file). Ifgetc
returned achar
, 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;
toint c;
so that it can hold the EOF marker when it is returned. Then, you must also change the while loop condition to check forEOF
instead ofNULL
.You could also call
feof(fr)
to test end of file separately from reading the character. If you did that, you could leavec
as achar
, but you would have to callfeof()
after you read the character but before you printed it out, and use abreak
to get out of the loop.如果不成功,
fgetc()
返回 EOF。If unsuccessful,
fgetc()
returns EOF.将其更改
为
换句话说:您需要与
EOF
进行比较,而不是与NULL
进行比较。您还需要使用int
变量来接收fgetc
的返回值。如果您使用char
,与EOF
的比较可能会失败,并且您将回到开始的地方。Change this
to
In other words: You need to compare against
EOF
, notNULL
. You also need to use anint
variable to receive the return value fromfgetc
. If you use achar
, the comparison withEOF
may fail, and you'll be back where you started.fgetc()
在文件末尾返回EOF
,而不是NULL
。fgetc()
returnsEOF
on end-of-file, notNULL
.将“NULL”替换为“EOF”。
Replace "NULL" with "EOF".
其他人已经解决了您遇到的问题,但使用
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 useputchar(c);
. There is quite a bit of overhead involved when you askprintf
to print just one character.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