476 Appendix B The Standard C Library
associated with filePtr. If the freopencall is successful,filePtris returned; other-
wise, a null pointer is returned.The freopenfunction is frequently used to reassign
stdin,stdout, or stderrin the program. For example, the call
if ( freopen ("inputData", "r", stdin) == NULL ) {
...
}
has the effect of reassigning stdinto the file inputData, which is opened in read
access mode. Subsequent I/O operations performed with stdinare performed with
the file inputData, as if stdinhad been redirected to this file when the program was
executed.
int fscanf (filePtr, format, arg1, arg2, ..., argn)
Reads data items from the file identified by filePtr, according to the format speci-
fied by the character string format.The values that are read are stored in the argu-
ments specified after format, each of which must be a pointer.The formatcharacters
that are allowed in formatare the same as those for the scanffunction (see Chapter
16).The fscanffunction returns the number of items successfully read and assigned
(excluding any %nassignments) or the value EOFif the end of file is reached before
the first item is converted.
int fseek (filePtr, offset, mode)
Positions the indicated file to a point that is offset(a long int) bytes from the
beginning of the file, from the current position in the file, or from the end of the file,
depending upon the value of mode(an integer). If modeequals SEEK_SET, positioning
is relative to the beginning of the file. If modeequals SEEK_CUR, positioning is relative
to the current position in the file. If modeequals SEEK_END, positioning is relative to
the end of the file.SEEK_SET,SEEK_CUR,and SEEK_ENDare defined in <stdio.h>.
On systems that distinguish between text and binary files,SEEK_ENDmight not be
supported for binary files. For text files, either offsetmust be zero or must be a
value returned from a prior call to ftell. In the latter case,modemust be SEEK_SET.
If the fseekcall is unsuccessful, a nonzero value is returned.
int fsetpos (filePtr, fpos)
Sets the current file position for the file associated with filePtrto the value pointed
to by fpos, which is of type fpos_t(defined in <stdio.h>). Returns zero on success,
and nonzero on failure. See also fgetpos.
long ftell (filePtr)
Returns the relative offset in bytes of the current position in the file identified by
filePtr, or –1L on error.
21 0672326663 AppB 6/10/04 2:03 PM Page 476