C 中的 rewind() 函數
rewind() 函數定義在<stdio.h>
頭文件。
原型:
void rewind(FILE *filename);
參數: FILE *filename
返回類型: void
函數的使用:
當我們處理文件時,有時我們需要啟動指定的文件。在文件處理中,我們使用 rewind() 函數將文件位置指示器移動到指定文件流的開頭。函數 rewind() 的原型是void rewind(FILE *filename);
這裏,filename
是文件指示器開始的文件的名稱。通過該函數,文件結束和錯誤標誌被清除。
C 中的 rewind() 示例
#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;
}
輸出

相關用法
- C語言 remove()用法及代碼示例
- C語言 rename()用法及代碼示例
- C語言 fread()用法及代碼示例
- C語言 feof()用法及代碼示例
- C語言 imagesize()用法及代碼示例
- C語言 getarcoords()用法及代碼示例
- C語言 strcspn()用法及代碼示例
- C語言 setlinestyle()用法及代碼示例
- C語言 showbits()用法及代碼示例
- C語言 sprintf()用法及代碼示例
- C語言 outtextxy()用法及代碼示例
- C語言 isgraph()用法及代碼示例
- C語言 grapherrormsg()用法及代碼示例
- C語言 moveto()用法及代碼示例
- C語言 putchar()用法及代碼示例
- C語言 tmpnam()用法及代碼示例
- C語言 putpixel()用法及代碼示例
- C語言 fillellipse()用法及代碼示例
- C語言 outtext()用法及代碼示例
- C語言 atan2()用法及代碼示例
注:本文由純淨天空篩選整理自Souvik Saha大神的英文原創作品 rewind() function in C language with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。