rewind()

Resets the file position indicator to the beginning of the file.

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

stream

A pointer for a file stream.

Remarks

The rewind() function sets the file indicator position of stream such that the next write or read operation will be from the beginning of the file. It also undoes any previous call to ungetc() and clears stream's end-of-file and error status.

This facility may have limited capability on configurations of the EWL that run on platforms that do not have console input/output or a file system.

Listing: Example of rewind() Usage

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

FILE *f;

char filename[80], buf[80];

// get a file name from the user

printf("Enter a file name to read.\n");

gets(filename);

// open a file for input

if (( f = fopen(filename, "r")) == NULL) {

printf("Can't open %s.\n", filename);

exit(1);

}

printf("Reading first line twice.\n");

// move the file position indicator to the beginning

// of the file

rewind(f);

// read the first line

fgets(buf, 80, f);

printf("Once: %s\n", buf);

// move the file position indicator to the

//beginning of the file

rewind(f);

// read the first line again

fgets(buf, 80, f);

printf("Twice: %s\n", buf);

// close the file

fclose(f);

return 0;

}

Output:

Enter a file name to read.

itwerks

Reading first line twice.

Once: flying fish and quail eggs

Twice: flying fish and quail eggs