C中的EOF,getc()和feof()
紧急行动
EOF代表文件结尾。如果getc()成功,该函数将返回EOF。
这是C语言中的EOF的示例,
假设我们有“new.txt”文件,其中包含以下内容。
This is demo! This is demo!
现在,让我们来看一个例子。
示例
#include <stdio.h>
int main() {
FILE *f = fopen("new.txt", "r");
int c = getc(f);
while (c != EOF) {
putchar(c);
c = getc(f);
}
fclose(f);
getchar();
return 0;
}输出结果
This is demo! This is demo!
在上述程序中,使用打开文件fopen()。当整数变量c不等于EOF时,它将读取文件。
FILE *f = fopen("new.txt", "r");
int c = getc(f);
while (c != EOF) {
putchar(c);
c = getc(f);
}getc()
它从输入中读取单个字符并返回一个整数值。如果失败,则返回EOF。
这是getc()C语言的语法,
int getc(FILE *stream);
这是getc()C语言的示例,
假设我们具有以下内容的“new.txt”文件-
This is demo! This is demo!
现在,让我们来看一个例子。
示例
#include <stdio.h>
int main() {
FILE *f = fopen("new.txt", "r");
int c = getc(f);
while (c != EOF) {
putchar(c);
c = getc(f);
}
fclose(f);
getchar();
return 0;
}输出结果
This is demo! This is demo!
在上述程序中,使用打开文件fopen()。当整数变量c不等于EOF时,它将读取文件。该功能getc()正在从文件中读取字符。
FILE *f = fopen("new.txt", "r");
int c = getc(f);
while (c != EOF) {
putchar(c);
c = getc(f);
}feof()
该功能feof()用于检查EOF之后文件的结尾。它测试文件结尾指示符。如果成功则返回非零值,否则返回零。
这是feof()C语言的语法,
int feof(FILE *stream)
这是feof()C语言的示例,
假设我们具有以下内容的“new.txt”文件-
This is demo! This is demo!
现在,让我们来看一个例子。
示例
#include <stdio.h>
int main() {
FILE *f = fopen("new.txt", "r");
int c = getc(f);
while (c != EOF) {
putchar(c);
c = getc(f);
}
if (feof(f))
printf("\n Reached to the end of file.");
else
printf("\n Failure.");
fclose(f);
getchar();
return 0;
}输出结果
This is demo! This is demo! Reached to the end of file.
在上述程序中,在上述程序中,使用打开文件fopen()。当整数变量c不等于EOF时,它将读取文件。该函数feof()再次检查指针是否已到达文件末尾。
FILE *f = fopen("new.txt", "r");
int c = getc(f);
while (c != EOF) {
putchar(c);
c = getc(f);
}
if (feof(f))
printf("\n Reached to the end of file.");
else
printf("\n Failure.");热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短