home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_06_03 / v6n3010a.txt < prev    next >
Text File  |  1989-09-28  |  3KB  |  94 lines

  1.  
  2. Figure 1:
  3.  
  4.  
  5. #define HEADER_LENGTH 256      /* Length of header record */
  6. #define DELETE_FLAG 0xFF       /* Record delete flag */
  7. #define RECORD_SIZE 11         /* Size of record */
  8. #define READ_ONLY_BINARY "rb"  /* Compiler dependent read binary mode */
  9.  
  10. /* The record has two fields:
  11.     Number (5 characters)
  12.     Name   (6 characters)
  13. */
  14.  
  15. #include "STDIO.H"
  16.  
  17. int main(argc, argv)
  18. /* Reads fixed length database file */
  19. /* The file is specified on the command line */
  20. /* e.g. readdata filename */
  21. int argc;
  22. char *argv[];
  23.     {
  24.     FILE *file;             /* Input file */
  25.     int ret;                /* Return from functions */
  26.     char buffer[100];       /* Input buffer */
  27.     int number;             /* Data item in record */
  28.     static char name[7];    /* Data item in record */ 
  29.     int record_number;      /* Current record number */
  30.  
  31.     record_number = 0;
  32.  
  33.     if (argc > 1)
  34.         {
  35.         file = fopen(argv[1], READ_ONLY_BINARY);
  36.         if (file != NULL)
  37.             {
  38.             ret = fseek(file, (long) HEADER_LENGTH, 0);
  39.             if (ret == 0)
  40.                 {
  41.                 /* Begin reading records. Break when read returns 
  42.                     less than record size */
  43.                 while (1)
  44.                     {
  45.                     ret = fread(buffer, 1, RECORD_SIZE, file);
  46.                     if (ret < RECORD_SIZE)
  47.                         {
  48.                         printf("\n End of file");
  49.                         break;
  50.                         }
  51.                     else 
  52.                         {
  53.                         record_number++;
  54.                         /* Check the delete flag */
  55.                         if (buffer[0] == DELETE_FLAG)
  56.                             {
  57.                             printf("\n Deleted record %d", record_number);
  58.                             }
  59.                         else 
  60.                             {
  61.                             /* Decode the record */
  62.                             ret = sscanf(buffer, "%5d%6s", &number, name);
  63.                             if (ret != 2)
  64.                                 {
  65.                                 printf("\n Error in record decoding %d ret %d",
  66.                                     record_number, ret);
  67.                                 }
  68.                             else 
  69.                                 {
  70.                                 /* Print decoded record */
  71.                                 printf("\n Record %d is %d %6s",
  72.                                     record_number, number, name);
  73.                                 }
  74.                             }
  75.                         }
  76.                     }
  77.                 }        /* End of successful header read */
  78.             else 
  79.                 {
  80.                 printf("\n Unable to read header record");
  81.                 }
  82.             }            /* End of successful open */
  83.         else 
  84.             {
  85.             printf("\n Unable to open file %s", argv[1]);
  86.             }
  87.         }                /* End of successful argument count */
  88.     else 
  89.         {
  90.         printf("\n Usage is readdata filename");
  91.         }
  92.     exit(0);
  93.     }
  94.