rewind ()- 调整当前文件位置

格式

#include <stdio.h>
void rewind(FILE *stream);

语言级别

ANSI

线程安全

描述

rewind() 函数将与 stream 关联的文件指针重新定位到文件的开头。 对 rewind() 函数的调用与以下内容相同:
   (void)fseek(stream, 0L, SEEK_SET);

rewind() 函数还会清除 的错误指示符。

使用 type=record打开的文件不支持 rewind() 函数。

返回值

没有返回值。

errno 的值可以设置为:
含义
EBADF
文件指针或描述符无效。
ENODEV
在错误的设备上尝试了操作。
EIOERROR
发生了不可恢复的I/O错误。
EIORECERR
发生了可恢复的I/O错误。

示例

此示例首先打开用于输入和输出的文件 myfile 。 它将整数写入文件,使用 rewind() 将文件指针重新定位到文件的开头,然后读取数据。
#include <stdio.h>
 
FILE *stream;
 
int data1, data2, data3, data4;
int main(void)
{
   data1 = 1; data2 = -37;
 
   /* Place data in the file */
   stream = fopen("mylib/myfile", "w+");
   fprintf(stream, "%d %d\n", data1, data2);
 
   /* Now read the data file */
   rewind(stream);
   fscanf(stream, "%d", &data3);
   fscanf(stream, "%d", &data4);
   printf("The values read back in are: %d and %d\n",
          data3, data4);
}
 
/********************  Output should be similar to:  **************
 
The values read back in are: 1 and -37
*/

相关信息