Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1.  
  2. #include "inc.h"
  3. #define LENGTH 103
  4.  
  5. int main(int argc, char* argv[]) {
  6.  
  7.  
  8.   FILE* infile;
  9.   char str[100];
  10.   int index;  
  11.   char *table[LENGTH];
  12.   int i = 0, j = 0;
  13.  
  14.   infile = fopen(argv[1], "r"); // read the file.
  15.  
  16.   if (infile == NULL) {
  17.  
  18.     perror("Cannot open the file specified.\n");
  19.     exit(1);
  20.  
  21.   }
  22.  
  23.  
  24.  
  25. // process file.  
  26.   printf("Data from file: \n");
  27.  
  28.   while (fgets(str, 100, infile) != NULL) {
  29.  
  30. // get the index for the current string.
  31.     index = hash(str, LENGTH);
  32.  
  33.     printf("Index for %s is %d\n", str, index);
  34.    
  35.     table[index] = strdup(str);
  36.    
  37.    
  38.   }
  39.  
  40.   fclose(infile);   // done with the file.
  41.  
  42. // report the string and locations in hash table.
  43.  
  44.   for (i = 0; table[i][j] != '\0'; ++i) {
  45.     if (table[i]) {
  46.      
  47.       for (j = 0; table[i][j] != '\0'; ++j)
  48.     printf("%c", table[i][j]);
  49.     }
  50.    
  51.   }
  52.  
  53.   return 0;  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement