Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

rewind - Position read/write pointer to beginning of file

&pagelevel(4)&pagelevel

Definition   

#include <stdio.h>

void rewind(FILE *fp);

rewind positions the read/write pointer of the file with file pointer fp to the beginning of the file.

Notes

The calls rewind(fp), fseek(fp,0L,SEEK_SET) and fseek64(fp,0LL,SEEK_SET) are equivalent, except that rewind does not return a value.

System files (SYSDTA, SYSOUT, SYSLST) cannot be positioned.

If new records are written to a text file (opened for creation or in append mode) and a rewind call is issued, any residual data is first written from the buffer to the file and terminated with an end-of-line character (\n).
Exception for ANSI functionality:
If the data of an ISAM file in the buffer does not end in a newline character, rewind does not cause a change of line (or change of record), i.e. the data is not automatically terminated with a newline character when writing from the buffer. Subsequent data lengthens the record in the file. When an ISAM file is read, therefore, only those newline characters explicitly written by the program are read in.

If the rewind function is called successfully, it deletes the EOF flag of the file and cancels all the effects of the preceding ungetc calls for this file.

Record I/O

rewind can also be used unchanged on files with record I/O.

Example

The following program first processes a file from the 11th character onwards to the end of the file and then continues at the beginning of the file (only works with binary files, i.e. in this case only with SAM and PAM files).

#include <stdio.h>
int main(void)
{
   FILE *fp;
   int c,i;
   fp = fopen("input","rb");
               /* skip the first 10 characters */
   fseek(fp,10L,SEEK_SET);
   while((c=getc(fp)) != EOF)
         putc((char)c,stdout);
               /* position to the beginning of the file */
   rewind(fp);
   for(i=0; i<10; i++)
      {
        c=getc(fp);
        putc((char)c,stdout);
      }
   fclose(fp);
   return 0;
}

See also

fseek, fseek64, fsetpos, fsetpos64