Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int load_words(char ***words, const char *filename)
- {
- if (!words || !filename)
- return 1;
- *words = malloc(sizeof(char**));
- FILE *f = fopen(filename, "rb");
- if (!f)
- {
- free(*words);
- *words = NULL;
- return 2;
- }
- int counter = 0;
- int len;
- while (!feof(f))
- {
- if (!fread(&len,1, sizeof(int), f) || len<=0)
- {
- //break;
- for (int i = 0; i<counter; i++)
- free(*((*words) + counter));
- free(*words);
- *words = NULL;
- fclose(f);
- return 3;
- }
- *((*words) + counter) = malloc(len+1);
- if (*(*words + counter) == NULL)
- {
- for (int i = counter - 1; i >= 0; i--)
- free(*(*words + counter));
- free(*words);
- *words = NULL;
- fclose(f);
- return 4;
- }
- for (int i = 0; i < len; i++)
- {
- if (feof(f))
- {
- for (int i = counter - 1; i >= 0; i--)
- free(*(*words + counter));
- free(*words);
- *words = NULL;
- fclose(f);
- return 3;
- }
- char t;
- fread(&t, sizeof(char), 1, f);
- *(*((*words) + counter) + i) = t;
- }
- *(*((*words) + counter) + len) = '\0';
- counter++;
- }
- //*(*words + counter) = NULL;
- fclose(f);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement