SHARE
TWEET

Untitled

a guest Jan 15th, 2020 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int load_words(char ***words, const char *filename)
  2. {
  3.     if (!words || !filename)
  4.         return 1;
  5.  
  6.     *words = malloc(sizeof(char**));
  7.  
  8.     FILE *f = fopen(filename, "rb");
  9.     if (!f)
  10.     {
  11.         free(*words);
  12.         *words = NULL;
  13.         return 2;
  14.     }
  15.     int counter = 0;
  16.     int len;
  17.     while (!feof(f))
  18.     {
  19.         if (!fread(&len,1, sizeof(int), f) || len<=0)
  20.         {
  21.             //break;
  22.             for (int i = 0; i<counter; i++)
  23.                 free(*((*words) + counter));
  24.             free(*words);
  25.             *words = NULL;
  26.             fclose(f);
  27.             return 3;
  28.         }
  29.         *((*words) + counter) = malloc(len+1);
  30.  
  31.         if (*(*words + counter) == NULL)
  32.         {
  33.             for (int i = counter - 1; i >= 0; i--)
  34.                 free(*(*words + counter));
  35.             free(*words);
  36.             *words = NULL;
  37.             fclose(f);
  38.             return 4;
  39.         }
  40.  
  41.         for (int i = 0; i < len; i++)
  42.         {
  43.             if (feof(f))
  44.             {
  45.                 for (int i = counter - 1; i >= 0; i--)
  46.                     free(*(*words + counter));
  47.                 free(*words);
  48.                 *words = NULL;
  49.                 fclose(f);
  50.                 return 3;
  51.             }
  52.  
  53.             char t;
  54.             fread(&t, sizeof(char), 1, f);
  55.             *(*((*words) + counter) + i) = t;
  56.         }
  57.         *(*((*words) + counter) + len) = '\0';
  58.         counter++;
  59.     }
  60.     //*(*words + counter) = NULL;
  61.     fclose(f);
  62.     return 0;
  63.  
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top