用 C 程序解释文件结尾 (EOF)
文件结束(EOF)表示输入的结束。
在我们输入文本后,如果我们按ctrl+Z,文本将终止,即它表示文件已到达结尾,没有可阅读的内容。
算法
请参阅下面给出的EOF算法。
Step 1: Open file in write mode. Step 2: Until character reaches end of the file, write each character in filepointer. Step 3: Close file. Step 4: Again open file in read mode. Step 5: Reading the character from file until fp equals to EOF. Step 5: Print character on console. Step 6: Close file.
示例
以下是文件结束(EOF)的C程序-
#include输出结果int main(){ char ch; FILE *fp; fp=fopen("std1.txt","w"); //以写模式打开文件 printf("enter the text then press cntrl Z:\n"); while((ch = getchar())!=EOF) //逐个字符读取字符直到它等于EOF{ i.e. when u press ctrlZ the while loop terminates putc(ch,fp); } fclose(fp); fp=fopen("std1.txt","r"); printf("text on the file:\n"); while ((ch=getc(fp))!=EOF) //从文件中读取字符直到fp等于EOF{ putchar(ch); } fclose(fp); return 0; }
执行上述程序时,会产生以下结果-
enter the text then press cntrl Z: This is the EOF demonstration example if your text typing is over press cntrlZ ^Z text on the file: This is the EOF demonstration example if your text typing is over press cntrlZ
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语