home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1989 / 06 / makenif.c < prev    next >
C/C++ Source or Header  |  1988-12-05  |  3KB  |  86 lines

  1. /*
  2.     MAKENIF.C   Creates ordered non-indexed random
  3.                 access file for use by SRCHNIF.C
  4.     Copyright (c) 1989 Ziff Communications Co.
  5.     PC Magazine * Ray Duncan
  6.  
  7.     The user is prompted to enter from zero to one hundred
  8.     ASCII strings.  These strings are then sorted and used 
  9.     to build records in the file TESTFILE.DAT.
  10.  
  11.     Each record in TESTFILE.DAT is RSIZE bytes long.  Bytes 
  12.     0 to (KSIZE-1) of the record are the ASCII key for searches
  13.     by SRCHNIF.C.  Bytes KSIZE to (RSIZE-1) of the record
  14.     are initialized to zero and are not used.
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <fcntl.h>
  21. #include <sys\types.h>
  22. #include <sys\stat.h>
  23. #include <io.h>
  24.  
  25. #define ISIZE   80                  /* max entry length */
  26. #define N_ITEMS 100                 /* max number of strings */
  27. #define RSIZE   64                  /* fixed record size */
  28. #define KSIZE    8                  /* maximum key length */
  29.  
  30. static char items[N_ITEMS * ISIZE]; /* entered keys stored here */
  31.  
  32. main(int argc, char *argv[])
  33. {
  34.     int i, j;                       /* some scratch variables */
  35.     int fh;                         /* handle for output file */
  36.     char rec[RSIZE];                /* scratch record buffer */
  37.  
  38.     puts("\nEnter keys for file records...");
  39.  
  40.     i = 0;                          /* initialize string count */
  41.  
  42.     while(i < N_ITEMS)              /* enforce maximum entries */
  43.     {
  44.         printf("%2d: ", i+1);       /* prompt user for next string */
  45.         gets(&items[ISIZE * i]);    /* read keyboard, store in array */
  46.  
  47.                                     /* last entry if empty line */
  48.         if(items[ISIZE * i] == 0) break;
  49.  
  50.         for(j = 0; j < i; j++)      /* make sure not duplicate key */
  51.         {
  52.             if(strncmp(&items[ISIZE * i], &items[ISIZE * j], KSIZE) == 0)
  53.                 break;              /* exit loop if duplicate found */
  54.         }
  55.  
  56.         if(i == j) i++;             /* count non-duplicate strings */
  57.         else puts("Duplicate key, try again.");
  58.     }
  59.  
  60.                                     /* sort strings if any entered */
  61.     if(i != 0) qsort(items, i, ISIZE, strcmp);
  62.  
  63.                                     /* create the output file */
  64.     fh = open("TESTFILE.DAT", O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, S_IWRITE);
  65.  
  66.     if(fh == -1)                    /* terminate if create failed */
  67.     {
  68.         puts("\nCan't create TESTFILE.DAT.");
  69.         exit(1);
  70.     }
  71.  
  72.     puts("\nWriting TESTFILE.DAT...");
  73.  
  74.     j = 0;                          /* initialize string counter */
  75.  
  76.     while (j < i)                   /* write file records */
  77.     {                               /* using strings as keys */
  78.         memset(rec, 0, RSIZE);
  79.         strncpy(rec, &items[ISIZE * j], KSIZE);
  80.         write(fh, rec, RSIZE);
  81.         j++;                        /* bump string counter */
  82.     }
  83.  
  84.     close(fh);                      /* release file handle */
  85. }
  86.