用 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 兄妹拍照祝福语简短的话