重新定位指向文件开头的文件指针。Repositions the file pointer to the beginning of a file.


      void rewind(  
   FILE *stream   


指向文件结构的指针。Pointer to FILE structure.


rewind 函数会将与 stream 关联的文件指针重新定位到文件开头。The rewind function repositions the file pointer associated with stream to the beginning of the file. rewind 的调用类似于A call to rewind is similar to

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

但是,与 fseek 不同,rewind 将清除流的错误指示符和文件尾指示符。However, unlike fseek, rewind clears the error indicators for the stream as well as the end-of-file indicator. 此外,与 fseek 不同,rewind 不返回指示是否已成功移动指针的值。Also, unlike fseek, rewind does not return a value to indicate whether the pointer was successfully moved.

若要清除键盘缓冲区,请将 rewind 与默认和键盘关联的流 stdin 结合使用。To clear the keyboard buffer, use rewind with the stream stdin, which is associated with the keyboard by default.

如果流是 NULL 指针,则调用无效的参数处理程序,如参数验证中所述。If stream is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则此函数将返回并将 errno 设置为 EINVALIf execution is allowed to continue, this function returns and errno is set to EINVAL.

有关这些代码及其他错误代码的信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrFor information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.


例程所返回的值Routine 必需的标头Required header
rewindrewind <stdio.h><stdio.h>

有关其他兼容性信息,请参见“简介”中的 兼容性For additional compatibility information, see Compatibility in the Introduction.


C 运行时库的所有版本。All versions of the C run-time libraries.


// 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  

请参阅See Also

流 I/OStream I/O