C语言教程
C语言控制语句
C语言函数
C语言数组
C语言指针
C语言字符串
C语言数学函数
C语言结构
C语言文件处理
C预处理器

C rewind()函数

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);//moves the file pointer at beginning of the file
while((c=fgetc(fp))!=EOF){
printf("%c",c);
}
fclose(fp);  
getch();  
}  
输出:
this is a simple textthis is a simple text
如您所见,rewind()函数将文件指针移动到文件的开头,这就是为什么"这是简单的文本"被打印两次的原因。如果不调用rewind()函数,则"这是简单的文本"将仅打印一次。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4