home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / file / svbin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-07  |  1.1 KB  |  53 lines

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