rewind() - C語言庫函式


C庫函式 void rewind(FILE *stream) 設定給定流的檔案的開頭的檔案位置。

宣告

以下是宣告rewind()  函式。

void rewind(FILE *stream)

引數

  • stream -- 這是一個檔案物件的標識流的指標。

返回值

這個函式不返回任何值。

例子

下面的例子演示了如何使用rewind() 函式。

#include <stdio.h>

int main()
{
   FILE *fp;
   int ch;

   fp = fopen("file.txt", "r");

   if( fp != NULL ) 
   {
      while( !feof(fp) )
      {
         ch = fgetc(fp);
         printf("%c", ch);
      }
      rewind(fp);

      while( !feof(fp) )
      {
         ch = fgetc(fp);
         printf("%c", ch);
      }
      fclose(fp);
   }

   return(0);
}

假設我們有一個文字檔案file.txt中有以下內容:

This is tw511.com

現在讓我們來編譯和執行上面的程式,這將產生以下結果:

This is tw511.com
This is tw511.com