/************************************************************************ * * * Copyright Digital Equipment Corporation 1998. All rights reserved. * * * * Restricted Rights: Use, duplication, or disclosure by the U.S. * * Government is subject to restrictions as set forth in subparagraph * * (c) (1) (ii) of DFARS 252.227-7013, or in FAR 52.227-19, or in FAR * * 52.227-14 Alt. III, as applicable. * * * * This software is proprietary to and embodies the confidential * * technology of Digital Equipment Corporation. Possession, use, or * * copying of this software and media is authorized only pursuant to a * * valid written license from Digital or an authorized sublicensor. * * * ************************************************************************/ /* chap_1_stream_record.c */ /* This program demonstrates the difference between */ /* record mode and stream mode Input/Output. */ #include #include #include void process_records(const char *fspec, FILE * fp); main() { FILE *fp; fp = fopen("example-fixed.dat", "w", "rfm=fix", "mrs=40", "rat=none"); if (fp == NULL) { perror("example-fixed"); exit(EXIT_FAILURE); } printf("Record mode\n"); process_records("example-fixed.dat", fp); fclose(fp); printf("\nStream mode\n"); fp = fopen("example-streamlf.dat", "w"); if (fp == NULL) { perror("example-streamlf"); exit(EXIT_FAILURE); } process_records("example-streamlf.dat", fp); fclose(fp); } void process_records(const char *fspec, FILE * fp) { int i, sts; char buffer[40]; /* Write records of all 1's, all 2's and all 3's */ for (i = 0; i < 3; i++) { memset(buffer, '1' + i, 40); sts = fwrite(buffer, 40, 1, fp); if (sts != 1) { perror("fwrite"); exit(EXIT_FAILURE); } } /* Rewind the file and write 10 characters of A's, then 10 B's, */ /* then 10 C's. */ /* */ /* For stream mode, each fwrite call outputs 10 characters */ /* and advances the file position 10 characters */ /* characters. */ /* */ /* For record mode, each fwrite merges the 10 characters into */ /* the existing 40-character record, updates the record and */ /* advances the file position 40 characters to the next record. */ rewind(fp); for (i = 0; i < 3; i++) { memset(buffer, 'A' + i, 10); sts = fwrite(buffer, 10, 1, fp); if (sts != 1) { perror("fwrite2"); exit(EXIT_FAILURE); } } /* Now reopen the file and output the records. */ fclose(fp); fp = fopen(fspec, "r"); for (i = 0; i < 3; i++) { sts = fread(buffer, 40, 1, fp); if (sts != 1) perror("fread"); printf("%.40s\n", buffer); } return; }