Chapter Contents |
Previous |
Next |
fsetpos |
Portability: | ISO/ANSI C conforming |
SYNOPSIS | |
DESCRIPTION | |
RETURN VALUE | |
CAUTIONS | |
IMPLEMENTATION | |
EXAMPLE | |
RELATED FUNCTIONS | |
SEE ALSO |
SYNOPSIS |
#include <stdio.h> int fsetpos(FILE *f, const fpos_t *pos);
DESCRIPTION |
fsetpos
positions the stream associated with the
FILE
object addressed by
f
to the position
specified by the object pointed to by
pos
.
This object is of type
fpos_t
, which is
defined in
stdio.h
.
The value of the object addressed by
pos
may have been stored by a previous call to
fgetpos
for the same stream, or it may have been constructed by some
other mechanism. The use of values that have not been generated by
fgetpos
is nonportable. See File positioning with fgetpos and fsetpos for information on
the interpretation of file-position values.
The
fsetpos
function
can be used with most files, accessed either as text or binary. Note that
it may be used to reposition files that
fseek
cannot process, including files accessed as a binary stream using
the
"seq"
access method. See OS/390 Files with Restricted Positioning and CMS Files with Restricted Positioning for file types that do not
fully support
fsetpos
.
After a call to
fsetpos
on a stream that permits both reading and writing, the next file operation
may be input or output.
RETURN VALUE |
If successful,
fsetpos
returns 0. If it fails,
fsetpos
returns a nonzero value and stores an appropriate error code in
errno
. See the list of
errno
values in The errno Variable.
Calls to
fsetpos
with an invalid
pos
may not be detected immediately, but such
calls will probably cause errors whenever the file is next read or written.
CAUTIONS |
If output occurs after a call to
fsetpos
, characters following the point of output
may be erased from the file. This occurs when
trunc=yes
is in effect. Therefore, when
trunc=yes
is in effect, you are unable to return to a previous file
position if output has been performed before that point.
See Opening Files for more information on the
trunc
amparm.
A program that makes direct use of the components of
an
fpos_t
value is not portable.
IMPLEMENTATION |
See File positioning with fgetpos and fsetpos for information on the implementation
of
fgetpos
and the structure of
fpos_t
values.
EXAMPLE |
This example illustrates how to use
fsetpos
and
fgetpos
to build and use an index file for access to individual lines of a
file:
#include <stdio.h> #include <string.h> #define KEYLEN 10 /* size of key in record */ #define DATALEN 70 /* size of data area in record */ #define TABSIZE 100 /* maximum number of records */ struct { char keyval[KEYLEN]; fpos_t location; } keytable[TABSIZE]; struct record { char keyval[KEYLEN]; char data[DATALEN]; }; int filesize; void bldtable(FILE *fileptr); int findrec(FILE *fileptr, char keyval[KEYLEN], struct record *input); main() { FILE *fileptr; struct record output; char key[KEYLEN] = "PAR-94412M"; /* key to be found */ /* Open data file and build index file. */ /* Example of information in data file: */ /* PAR-97612MPearl & Black $325.00 */ /* PAR-94412MMarbled Green $275.00 */ if ((fileptr = fopen("ddn:DATA", "rb")) != NULL) bldtable(fileptr); else{ puts("Unable to open input file."); exit(99); } /* Find desired key. */ if ( !findrec(fileptr, key, &output) ) printf("Data area associated with key %.*s is: %.*s\n", KEYLEN, key, DATALEN, output.data); else puts("Unable to find matching key."); } /* Build the table for key and record addresses. */ void bldtable(FILE *fileptr) { struct record input; int index = 0; for (;;){ /* Store file pointer location. */ fgetpos(fileptr, &keytable[index].location); /* Read one record. */ fread(&input, sizeof(struct record), 1, fileptr); if (feof(fileptr) || ferror(fileptr)) break; /* Save the keyval. */ memcpy(keytable[index].keyval, input.keyval, KEYLEN); ++index; } filesize = index; return; } /* Find the key. */ int findrec(FILE *fileptr, char keyval[KEYLEN], struct record *input) { int index; /* Search keytable for specified keyval. */ for(index = 0; index < filesize; ++index) if (memcmp(keyval, keytable[index].keyval,KEYLEN) == 0) break; /* Was the key found? */ if (index >= filesize) return -1; /* If found, read complete record from file. */ fsetpos(fileptr, &keytable[index].location); fread(input, sizeof(struct record), 1, fileptr); return 0;
RELATED FUNCTIONS |
SEE ALSO |
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.