English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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 simple text”要打印2次。如果不调用rewind()函数,“this is simple text”将只打印一次。