rewind [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation
HP C/iX Library Reference Manual
rewind
Sets the file position indicator for a stream to the beginning of the
file.
Syntax
#include <stdio.h>
void rewind (FILE *stream);
Parameters
stream A pointer to an open stream.
Return Values
None.
Description
The rewind function sets the file position indicator for the specified
stream to the beginning of the file.
NOTE If you have a stream open for both reading and writing, a read
operation cannot be followed by a write operation without one of
the following occurring first: a rewind(), an fseek(), or a read
operation that encounters end-of-file. Similarly, a write
operation cannot be followed by a read operation unless a rewind()
or fseek() is performed.
Example
Suppose you sometimes wish to use a password on a data file accessed by
an application program. This password is to be optionally stored in
encrypted form on the first line of the file. The line is recognized as
a password line if the first two characters are "*P". If the file has no
password line, access to the file is unrestricted. If a password line is
found, the program prompts for the password before permitting access.
The following code looks for a password line:
#include <stdio.h>
main(argc, argv)
int argc;
char *argv[ ];
{
FILE *pswd;
char line[256];
if(argc != 2) {
fprintf(stderr, "Usage: getpswd file\n");
exit(1);
}
pswd = fopen(argv[1], "r");
if(pswd == NULL) {
fprintf(stderr, "Can't open %s.\n", argv[1]);
exit(1);
}
fgets(line, 256, pswd);
if(line[0] == '*' && line[1] == 'P') {
/* ask for and check password */
} else
rewind(pswd);
/* application program goes here */
exit(0);
}
If the first two characters of the first line are "*P", the code that
asks for and checks a password is executed. However, if the first line
is not a password line, the file is assumed to be unprotected. Thus, the
file must be rewound so the data contained in the first line is available
to the application program.
See Also
ftell(), fseek(), ANSI C 4.9.9.5, POSIX.1 8.1
MPE/iX 5.0 Documentation