rewindrewind

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

语法Syntax

void rewind(
   FILE *stream
);

参数Parameters

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

备注Remarks

倒带函数将与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不同,倒带会清除流的错误指示符和文件尾指示符。However, unlike fseek, rewind clears the error indicators for the stream as well as the end-of-file indicator. 另外,与fseek不同的是,倒带不会返回一个值以指示是否已成功移动指针。Also, unlike fseek, rewind does not return a value to indicate whether the pointer was successfully moved.

若要清除键盘缓冲区,请在默认情况下,使用与键盘关联的流stdin倒带To clear the keyboard buffer, use rewind with the stream stdin, which is associated with the keyboard by default.

如果 stream 为指针,则将调用无效参数处理程序,如参数验证中所述。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.

要求Requirements

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

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

Libraries

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

示例Example

// 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 );
   }
}

OutputOutput

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

请参阅See also

流 I/OStream I/O