rewind() — Set file position to beginning of file

Standards

Standards / Extensions C or C++ Dependencies
ISO C
POSIX.1
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3 Language Environment®
both  

Format

#include <stdio.h>

void rewind(FILE *stream);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <stdio.h>

void rewind_unlocked(FILE *stream);

General description

Repositions the file position indicator of the stream pointed to by stream. A call to rewind() is the same as the statement below, except that rewind() also clears the error indicator for the stream.
   (void) fseek(stream, 0L, SEEK_SET);

rewind_unlocked() is functionally equivalent to rewind() with the exception that it is not thread-safe. This function can safely be used in a multithreaded application if and only if it is called while the invoking thread owns the (FILE*) object, as is the case after a successful call to either the flockfile() or ftrylockfile() function.

Returned value

rewind() returns no values.

If an error occurs, errno is set. After the error, the file position does not change. The next operation may be either a read or a write operation.

Special behavior for XPG4.2: The rewind() function returns -1 and sets errno to ESPIPE if the underlying file type for the stream is a PIPE or a socket.

Example

CELEBR14
/* CELEBR14                                      

   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                
   the data back in.                                                            
                                                                                
 */                                                                             
#include <stdio.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   FILE *stream;                                                                
   int data1, data2, data3, data4;                                              
   data1 = 1; data2 = -37;                                                      
                                                                                
      /* Place data in the file */                                              
   stream = fopen("myfile.dat", "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
The values read back in are: 1 and -37

Related information