用 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 官宣新年祝福语简短