Advertisement
Guest User

speller-dictionary.c

a guest
Apr 30th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.41 KB | None | 0 0
  1. typedef struct node {
  2.     char *val;
  3.     struct node * next;
  4. } node_t;
  5.  
  6. /**
  7.  * Returns true if word is in dictionary else false.
  8.  */
  9. bool check(const char *word)
  10. {
  11.    
  12.     return false;
  13.    
  14. }
  15.  
  16. /**
  17.  * Loads dictionary into memory. Returns true if successful else false.
  18.  */
  19. bool load(const char *dictionary)
  20. {
  21.    
  22.     FILE *file = fopen(dictionary, "r");
  23.     char * buffer = 0;
  24.     long length;
  25.  
  26.     if (file)
  27.     {
  28.         fseek (file, 0, SEEK_END);
  29.         length = ftell (file);
  30.         fseek (file, 0, SEEK_SET);
  31.         buffer = malloc (length);
  32.             if (buffer)
  33.             {
  34.                 fread (buffer, 1, length, file);
  35.             }
  36.    
  37.         fclose (file);
  38.     }
  39.  
  40.     char *token = NULL;
  41.  
  42.     char *delim = "\n";
  43.     int i = 0;
  44.    
  45.     node_t * head = NULL;
  46.     head = malloc(sizeof(node_t));
  47.     if (head == NULL) {
  48.         return 1;
  49.     }
  50.    
  51.     token = strtok(buffer, delim);
  52.    
  53.     head->val = token;
  54.     head->next = NULL;
  55.    
  56.     while (token != NULL){
  57.             // printf("%d The token is:  %s\n", i, token);
  58.            
  59.             head->next = malloc(sizeof(node_t));
  60.             head->next->val = token;
  61.             head->next->next = NULL;
  62.            
  63.             //printf("%d The token is:  %s\n", i, &head->next->val);
  64.            
  65.             token = strtok(NULL, delim );
  66.             i++;
  67.         }
  68.    
  69.     return true;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement