HP 3000 Manuals

fread [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation


HP C/iX Library Reference Manual

fread 

Reads data items from an open stream.

Syntax 

     #include <stdio.h>
     size_t fread (void *fileptr, size_t size, size_t nitems,
        FILE *stream);

Parameters 

fileptr       A pointer to a buffer to hold the data.  The type of the
              buffer is determined by the type of the data being read.

size          The size of each data item, in bytes.

nitems        The number of data items to read.

stream        A pointer to an open stream.

Return Values 

>0            The number of items actually read.

0             Either EOF was detected or an error occurred.

Description 

The fread function reads nitems times size number of bytes into the
buffer pointed to by the fileptr argument.  The bytes are read from the
stream pointed to by the stream argument.

This function returns the number of items actually read.  This may be
less than the number of requested items if an error occurs or an end of
file is encountered.


NOTE You must use special care when using fread() to access ASCII files from the MPE/iX file system. These files are record-oriented text streams. The fread function returns a new line character, \n, whenever it reaches an end of record. You must take the \n character into account when reading, writing, or repositioning within an MPE/iX fixed length ASCII file. Binary MPE files behave differently from ASCII MPE files. See chapter 2, "HP C/iX Library Input and Output," for more information.
Example The following program keeps track of employee data. Each employee is described in a single structure. #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; { : } This program reads the employee information contained in the binary file empdata. The data in this file consists of concatenated streams of bytes describing each employee in a 400-employee company. The bytes are written such that, when read correctly, they correspond exactly with the emp structure defined in the program. The staff array is an array of structures containing one structure for each employee. In the fread() call, the sizeof(staff[0]) expression returns the number of bytes in the emp structure. Because the same number of bytes are in each employee structure, any element of the staff array can be specified as the sizeof argument; staff[0] is used in this example. By counting the number of bytes in each structure member, you can approximate the number of bytes returned by the sizeof operator (in this example, 40 + 40 + 8 + 6 + 2 + 4 = 100 bytes). This might vary due to padding performed by a programming language or by machine architecture. Specifying EMPS as the nitems argument tells fread to read 400 structures. Thus, 100 x 400 = 40000 bytes are read, filling in the information for the members of each structure contained in the staff array. The fread() and fwrite() functions can read or write any type of data. The following examples show some fread() calls that read different types of data: To read a long integer: long nint; fread((void *)&nint, sizeof(nint), 1, stream); To read an array of 100 long integers: long nint[100]; fread((void *)nint, sizeof(nint[0]), 100, stream); To read a double-precision floating-point value: double fpoint; fread((void *)&fpoint, sizeof(fpoint), 1, stream); To read an array of 50 floating-point values: float fpoint[50]; fread((void *)fpoint, sizeof(fpoint[0]), 50, stream); See Also fgetc(), getc(), gets(), getchar(), fscanf(), scanf(), ANSI C 4.9.8.1, POSIX.1 8.1


MPE/iX 5.0 Documentation