用 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 教师闺蜜祝福语简短