Chapter Contents

Previous

Next
afwriteh

afwriteh



Write 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 afwriteh(const void *ptr, size_t size,
                size_t count, FILE *f);


DESCRIPTION

afwriteh writes items to the stream associated with the FILE object that f addresses. size defines the size of each item, count defines the number of items to be written, and ptr addresses the area containing the items. If all the items do not fit into the current record, a diagnostic message is generated and the file's error flag is set.

Calls to afwriteh to write items of type typeval commonly have this form:

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

You can only use afwriteh with a binary stream. See Augmented Standard I/O for more information.


RETURN VALUE

afwriteh returns the number of items written. If there are too many items, only those that fit are written.


CAUTION

When used on a file with relative attributes, afwriteh behaves exactly like fwrite because such a file is 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.


DIAGNOSTICS

afwriteh never writes more than a single record; an error occurs if there is no room in the current record for all items.

The return value from afwriteh does not indicate whether the call is completely successful. You can use the ferror function to determine whether an error occurs.


EXAMPLE

This example writes a customer record to be read by the afreadh example:

#include <lcio.h>
#include <stdlib.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;

   int customers[] = {
      1001, 1002, 1003, 1004};
   struct custrec custinfo[] = {
      { "Paul Barnes", "256 Oak Street, Cary, NC" },
      { "Janice Palmer", "1500 Pine Avenue, Austin, TX" },
      { "Frank Smith", "92 Maple Boulevard, Concord, NH" },
      { "Carlotta Perez", "634 First Street, Los Angeles, CA" }
    };
   payrec payment[] = { 54.40, 234, 16.81, 523};

   struct hdr cust_hdr;
   int i;

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

      /* Write out one customer record and one payment record */
      /* for each customer.                                   */
   for (i = 0; i < sizeof(customers)/sizeof(customers[0]); ++i){
      cust_hdr.custno = customers[i];
      cust_hdr.type = 'C';
      afwriteh(&cust_hdr, sizeof(cust_hdr), 1, custf);
      if (ferror(custf)) exit(1);
      afwrite(&custinfo[i], sizeof(struct custrec), 1, custf);
      if (ferror(custf)) exit(1);
      cust_hdr.type = 'P';
      afwriteh(&cust_hdr, sizeof(cust_hdr), 1, custf);
      if (ferror(custf)) exit(1);
      afwrite(&payment[i], sizeof(payrec), 1, custf);
      if (ferror(custf)) exit(1);
   }
   fclose(custf);
   printf("%d customer records and %d payment records written.\n",
          sizeof(customers)/sizeof(customers[0]),
          sizeof(customers)/sizeof(customers[0]));
   exit(0);
}


RELATED FUNCTIONS

afwrite


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

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