rewind() — Adjust Current File Position

Format

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

Language Level: ANSI

Threadsafe: Yes.

Description

The rewind() function repositions the file pointer associated with stream to the beginning of the file. A call to the rewind() function is the same as:

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

except that the rewind() function also clears the error indicator for the stream.

The rewind() function is not supported for files opened with type=record.

Return Value

There is no return value.

The value of errno may be set to:

Value
Meaning
EBADF
The file pointer or descriptor is not valid.
ENODEV
Operation attempted on a wrong device.
EIOERROR
A non-recoverable I/O error occurred.
EIORECERR
A recoverable I/O error occurred.

Example that uses rewind()

This example first opens a file myfile for input and output. It writes integers to the file, uses rewind() to reposition the file pointer to the beginning of the file, and then reads in the data.

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

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]