home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc / qc25 / svbin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-15  |  1.1 KB  |  48 lines

  1. /* SVBIN.C: Saves integer variables in binary format. */
  2. #include <stdio.h>
  3. #define ASIZE 10
  4.  
  5. main()
  6. {
  7.    FILE *ap;
  8.    int zebra[ASIZE], acopy[ASIZE], bcopy[ASIZE];
  9.    int i;
  10.  
  11.    for( i = 0; i < ASIZE; i++ )
  12.       zebra[i] = 7700 + i;
  13.  
  14.    if( (ap = fopen( "binfile", "wb" )) != NULL )
  15.    {
  16.       fwrite( zebra, sizeof(zebra), 1, ap );
  17.       fclose( ap );
  18.    }
  19.    else
  20.       perror( "Write error" );
  21.  
  22.    if( (ap = fopen( "morebin", "wb" )) != NULL )
  23.    {
  24.       fwrite( &zebra[0], sizeof(zebra[0]), ASIZE, ap );
  25.       fclose( ap );
  26.    }
  27.    else
  28.       perror( "Write error" );
  29.  
  30.    if( (ap = fopen( "binfile", "rb" )) != NULL )
  31.    {
  32.       printf( "Hexadecimal values in binfile:\n" );
  33.       while( (i = fgetc( ap )) != EOF )
  34.          printf( "%02X ", i );
  35.       rewind( ap );
  36.       fread( acopy, sizeof(acopy), 1, ap );
  37.       rewind( ap );
  38.       fread( &bcopy[0], sizeof( bcopy[0] ), ASIZE, ap);
  39.       for( i=0; i<ASIZE; i++ )
  40.          printf( "\nItem %d = %d\t%d", i, acopy[i], bcopy[i] );
  41.       fclose( ap );
  42.  
  43.    }
  44.    else
  45.       perror( "Read error" );
  46.  
  47. }
  48.