rewind -- Position to Start of File


SYNOPSIS
#include <stdio.h>
void rewind(FILE *f);
DESCRIPTION
rewind
positions the stream associated with the FILE
object addressed by f
to its first character. It also resets
the error flag for the stream if it is set.
RETURN VALUE
rewind
has no return value.
EXAMPLE
This example searches for the nth record in a file, returns
that record, and rewinds the file after the search is finished:
#include <stdio.h>
#define RECLEN 80
FILE *f; /* file to be searched */
char *search(int); /* prototype of the search function */
main()
{
int n ; /* The record number to be found */
/* points to the address of a copy of */
/* the returned record. */
char *addr;
/* Ask the user for the number of the record to */
/* be found. */
puts("Which record do you want to read?");
scanf("%d", &n);
addr = search(n);
printf("The record is %sn", *addr);
}
/* performs the search and rewind of the file f */
char *search(int n){
char *record; /* points to a copy of the record */
int i;
while (!feof(f)) {
/* Read the records until the nth one is found. */
for (i=0; i <= n; i++)
afread(record, RECLEN, 1, f);
}
/* Reposition the stream to the top of f. */
rewind(f);
/* Return the address of the copy of the record. */
return record;
}
RELATED FUNCTIONS
fseek
, fsetpos
SEE ALSO