HP C/iX Library Reference Manual (30026-90004)
186 Chapter5
HP C/iX Library Function Descriptions
fseek
fseek to do this:
…
int empno, bytes;
long total;
FILE *data;
struct emp empinfo;
/* check for usage error and open data file */
…
sscanf(argv[1], “%d”, &empno);
bytes = sizeof(empinfo);
total = (empno - 1) * bytes;
fseek(data, total, 0);
fread((char *)&empinfo, sizeof(empinfo), 1, data);
/* print out desired information */
…
exit(0);
}
In this program, argv[1] contains, using a command-line argument, the employee number
about whom information is desired. The employee number is converted to integer form
using sscanf. The number of bytes per employee structure is obtained using sizeof, and
is stored in bytes. The total number of bytes to skip in the data file is found by
multiplying the employee number (minus one) times the number of bytes per employee
structure. This is stored in total. Then, fseek() is used to seek past the specified
number of bytes, relative to the beginning of the data file. This leaves the next I/O
operation positioned at the start of the specified employee's information. The information
is read using fread().
See Also
ftell(), rewind(), ANSI C 4.9.9.2, POSIX.1 8.1