c语言中rewind函数
C中的rewind()函数 (rewind() function in C)
The rewind() function is defined in the <stdio.h> header file.
rewind()函数在<stdio.h>头文件中定义。
Prototype:
原型:
void rewind(FILE *filename);
Parameters: FILE *filename
参数: FILE *文件名
Return type: void
返回类型: void
Use of function:
使用功能:
When we are dealing with files then sometimes we need to start of the specified files. In file handling, we use rewind() function to move the file position indicator to start of the specified file stream. The prototype of the function rewind() is void rewind(FILE *filename);
当我们处理文件时,有时我们需要启动指定的文件。 在文件处理中,我们使用rewind()函数将文件位置指示符移动到指定文件流的开始。 函数rewind()的原型为void rewind(FILE * filename);
Here, filename is the name of the file where the file indicator starts. By the function, end-of-file and error flag is cleared.
在这里, filename是文件指示符开始处的文件名。 通过该功能,文件结束和错误标志被清除。
C中的rewind()示例 (rewind() example in C)
#include <stdio.h>
#include <stdlib.h>
int main(){
//Initialize the file pointer
FILE *f;
char ch[100];
//Create the file for write operation
f=fopen("includehelp.txt","w");
printf("Enter five strings\n");
for(int i=0;i<4;i++){
//take the strings from the users
scanf("%[^\n]",&ch);
//write back to the file
fputs(ch,f);
//every time take a new line for the new entry string
//except for last entry.Otherwise print the last line twice
fputs("\n",f);
//clear the stdin stream buffer
fflush(stdin);
}
//take the strings from the users
scanf("%[^\n]",&ch);
fputs(ch,f);
//close the file after write operation is over
fclose(f);
//open a file
f=fopen("includehelp.txt","r");
printf("\n...............print the strings..............\n");
while(!feof(f)){
//takes the first 100 character in the character array
fgets(ch,100,f);
//and print the strings
printf("%s",ch);
}
rewind(f);
printf("\n...............print the strings again..............\n");
while(!feof(f)){
fgets(ch,100,f);
printf("%s",ch);
}
//close the file
fclose(f);
return 0;
}
Output
输出量
翻译自: https://www.includehelp.com/c-programs/rewind-function-in-c-language-with-example.aspx
c语言中rewind函数