HP C/iX Library Reference Manual (30026-90004)

170 Chapter5
HP C/iX Library Function Descriptions
fread
#include <stdio.h>
struct emp {
char name[40]; /* name */
char job[40]; /* job title */
long salary; /* salary */
char hire[6] /* hire date */
char curve[2] /* pay curve */
int rank; /* percentile ranking */
}
#define EMPS 400 /* no. of employees */
main()
{
int items;
struct emp staff[EMPS];
FILE *data;
data = fopen("empdata", "r");
if(data == NULL) {
fprintf(stderr, "Can't open employee data file.\n");
exit(1);
}
items = fread((char *)staff, sizeof(staff[0]), EMPS, data);
if(items != EMPS) {
fprintf(stderr, "Insufficient data found.\n");
exit(1);
}
fclose(data);
archive("empdata");
/* Employee information processing goes here. */
/* Processing is done. Write out new employee records. */
data = fopen("empdata", "w");
if(data == NULL) {
fprintf(stderr, "Can't create new employee file.\n");
exit(1);
}
items = fwrite((char *)staff, sizeof(staff[0]), EMPS, data);
if(items != EMPS) {
fprintf(stderr, "Write error!\n");
exit(1);
}
exit(0);
}
archive(filename)
char *filename;
{