rewind()
函數將檔指針設置在流的開頭。在需要多次使用流時,這就很有用。
rewind()
函數的語法:
void rewind(FILE *stream)
示例:
創建一個原始檔案:rewind-file.c,其代碼如下所示 -
#include<stdio.h>
void main() {
FILE *fp;
char c;
fp = fopen("string-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);
}
創建一個文本檔:string-file.txt,內容如下 -
this is rewind()function from zaixian tutorials.
執行上面示例代碼後,得到以下結果 -
this is rewind()function from zaixian tutorials.
this is rewind()function from zaixian tutorials.
如上所示,rewind()
函數將檔指針移動到檔的開頭,這就是為什麼檔string-file.txt中的內容被列印2
次。 如果不調用rewind()
函數,檔中的內容將只列印一次。
上一篇:
C語言fseek()函數
下一篇:
C語言ftell()函數