C 庫函數– rewind()
描述
C庫函數void rewind(FILE *stream)設置文件位置為給定流stream的文件的開頭。
聲明
下面是rewind() 函數的聲明。
void rewind(FILE *stream)
參數
- stream --這是指向FILE對象的指針,該FILE對象標識了流。
返回值
該函數不返回任何值。
實例
下面的實例演示了rewind() 函數的用法。
#include <stdio.h> int main() { char str[] = "This is w3cschool.cc"; FILE *fp; int ch; /* 首先让我们在文件中写入一些内容 */ fp = fopen( "file.txt" , "w" ); fwrite(str , 1 , sizeof(str) , fp ); fclose(fp); fp = fopen( "file.txt" , "r" ); while(1) { ch = fgetc(fp); if( feof(fp) ) { break ; } printf("%c", ch); } rewind(fp); printf("\n"); while(1) { ch = fgetc(fp); if( feof(fp) ) { break ; } printf("%c", ch); } fclose(fp); return(0); }
假設我們有一個文本文件file.txt ,它的內容如下:
This is w3cschool.cc
讓我們編譯並運行上面的程序,這將產生以下結果:
This is w3cschool.cc This is w3cschool.cc