rewind()函数将文件指针设置在流的开头。如果必须多次使用流,这很有用。
语法:
void rewind(FILE *stream)
示例:
档案:file.txt
this is a simple text
文件:rewind.c
#include<stdio.h>#include<conio.h> void main(){ FILE *fp; char c; clrscr(); fp=fopen("file.txt","r"); while((c=fgetc(fp))!=EOF){ printf("%c",c); } rewind(fp);//将文件指针移动到文件的开头 while((c=fgetc(fp))!=EOF){ printf("%c",c); } fclose(fp); getch(); }
输出:
this is a simple textthis is a simple text
如您所见,rewind()函数将文件指针移动到文件的开头,这就是为什么"this is a simple textthis is a simple text"被打印2次的原因。如果不调用rewind()函数,则"this is a simple text"将仅打印一次。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)