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 xuhuhu.com"; 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 xuhuhu.com
讓我們編譯並運行上面的程式,這將產生以下結果:
This is xuhuhu.com This is xuhuhu.com