rewind

將檔案指標重新置放到檔案開頭。

語法

void rewind(
   FILE *stream
);

參數

stream
FILE 結構的指標。

備註

函式會將 rewind 與 檔案 stream 開頭相關聯的檔案指標重新置放。 的 rewind 呼叫類似于

(void) fseek(stream, 0L, SEEK_SET );

不過,不同于 fseekrewind 會清除資料流程的錯誤指標和檔案結尾指標。 此外,不同于 fseekrewind 不會傳回值,指出指標是否成功移動。

若要清除鍵盤緩衝區,請使用 rewind 資料流程 stdin ,預設會與鍵盤相關聯。

如果 stream 是 NULL 指標,則會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,此函式會傳回 ,並 errno 設定為 EINVAL

如需這些錯誤碼和其他錯誤碼的相關資訊,請參閱 errno_doserrno_sys_errlist_sys_nerr

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

需求

常式 必要的標頭
rewind <stdio.h>

如需相容性詳細資訊,請參閱相容性

程式庫

所有版本的 C 執行階段程式庫

範例

// crt_rewind.c
/* This program first opens a file named
* crt_rewind.out for input and output and writes two
* integers to the file. Next, it uses rewind to
* reposition the file pointer to the beginning of
* the file and reads the data back in.
*/
#include <stdio.h>

int main( void )
{
   FILE *stream;
   int data1, data2;

   data1 = 1;
   data2 = -37;

   fopen_s( &stream, "crt_rewind.out", "w+" );
   if( stream != NULL )
   {
      fprintf( stream, "%d %d", data1, data2 );
      printf( "The values written are: %d and %d\n", data1, data2 );
      rewind( stream );
      fscanf_s( stream, "%d %d", &data1, &data2 );
      printf( "The values read are: %d and %d\n", data1, data2 );
      fclose( stream );
   }
}

輸出

The values written are: 1 and -37
The values read are: 1 and -37

另請參閱

資料流 I/O