Chapter Contents

Previous

Next
afreadh

afreadh



Read Part of a Record

Portability: SAS/C extension


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
DIAGNOSTICS
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <lcio.h>

size_t afreadh(void *ptr, size_t size, size_t count, FILE *f);


DESCRIPTION

afreadh reads up to count items from the current record of the stream associated with the FILE object that f addresses. size defines the size of each item, and ptr addresses the area into which the items will be read.

Calls to afreadh to obtain items of type typeval commonly have this form:

typeval buf[count];
afreadh(buf, sizeof(typeval), count, f);

afreadh can only be used with a binary stream. See Augmented Standard I/O for more information.


RETURN VALUE

afreadh returns the number of items read from the record (which may be less than the maximum).


CAUTION

When used on a file with relative attributes, afreadh behaves exactly like fread because these files are processed as a continuous stream of characters without record boundaries. To process a file with relative attributes on a record-by-record basis, you must open it with afopen and specify the "seq" access method.

If afreadh reads a zero-length record, it skips it and ignores it. Use the afread0 function if you are processing a file that may contain zero-length records.


DIAGNOSTICS

afreadh never reads past the end of the current record; an error occurs if the record contains a fractional number of items.

The return value from afreadh does not indicate whether the call was completely successful. You can use the ferror function to determine whether an error occurred.


EXAMPLE

#include <lcio.h>
#define NAMESIZE 30
#define ADDRSIZE 80

main()
{
   FILE *custf;
   struct hdr {
      int custno;
      char type;
   };
   struct custrec {
      char name[NAMESIZE];
      char addr[ADDRSIZE];
   };
   typedef double payrec;

   struct hdr header;
   struct custrec customer;
   payrec payment;

   custf = fopen("tso:custfile", "rb");
   if (!custf) exit(1);

   for (;;)
      {
            /* Read customer number and record type.         */
         afreadh(&header, sizeof(header), 1, custf);
         if (feof(custf) || ferror(custf)) break;
         if (header.type == 'C'){        /* a customer record */
                  /* Read rest of customer record. */
            afread(&customer, sizeof(customer), 1, custf);

            if (feof(custf) || ferror(custf)) break;
              printf("Customer record %d read:\n"
                     "Name: %s\nAddress: %s\n", header.custno,
                     customer.name, customer.addr);
            }
         else if (header.type == 'P'){    /* a payment record */
            afread(&payment, sizeof(payment), 1, custf);
            if (feof(custf) || ferror(custf)) break;
            printf("Payment record for customer %d read:\n"
                   "Amount: %.2f\n", header.custno, payment);
            }
         else{
            printf("Unknown record type %c, aborting.\n",
                   header.type);
            abort();
            }
      }

      if (ferror(custf)){
            puts("Aborting due to error reading file.");
            abort();
         }
      fclose(custf);
      exit(0);
}


RELATED FUNCTIONS

afread


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.