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


void rewind(
   FILE *stream


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


Rewind函数将与关联的文件指针重新定位到文件的开头。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 ( ,0 L,SEEK_SET);(void) fseek( stream, 0L, SEEK_SET );

但是,与不同fseekrewind清除流的错误指示符以及文件尾指示器。However, unlike fseek, rewind clears the error indicators for the stream as well as the end-of-file indicator. 此外,与不同fseekrewind不返回一个值以指示是否已成功移动指针。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.


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