/* Vladimir Poplavskij. Wykladowca * ----------------- * Czytamy tekst. * ----------------- 2022-12-22 */ #include <stdlib.h> #include <string.h> #define MAX_LINE_LENGTH 10240 #define MAX_WORDS 502400 #define BUFFER_SIZE 1024 int main(void) { char* words[MAX_WORDS]; FILE* fp = fopen("dane.txt", "rb"); if (fp == NULL) { perror("Error opening file"); } char* buffer = (char*)malloc(BUFFER_SIZE); if (buffer == NULL) { perror("Error allocating buffer"); } size_t bytes_read; char * array[MAX_LINE_LENGTH]; int i=0; static int word_count = 0; while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, fp)) > 0) { array[i] = (char*)malloc(BUFFER_SIZE + 1); memcpy(array[i], buffer, BUFFER_SIZE); char* word = strtok(array[i], "\n"); while (word != NULL) { words[word_count] = (char*)malloc(strlen(word) + 1); strcpy(words[word_count], word); word_count++; // Get the next word word = strtok(NULL, "\n"); } i++; } if (ferror(fp)) { perror("Error reading file"); } printf("%d \n %d \n %s \n", i, word_count, array[0]); *(total + 0) = word_count; free(buffer); fclose(fp); }