Node *index_file(Node *head, char *fname, char **filenames) { char line[MAXLINE]; char *marker, *token; int countlines = 0; FILE *fp; if((fp = fopen(fname, "r")) == NULL) { perror(fname); exit(1); } while((fgets(line, MAXLINE, fp)) != NULL) { countlines++; if((countlines % 1000) == 0) { printf("processed %d lines from %s (words%d)\n", countlines, fname, num_words); } line[strlen(line)-1] = '\0'; if(strlen(line) == 0) { continue; } marker = line; while((token = strsep(&marker, " \t")) != NULL) { if(strlen(token) == 0) { continue; } token = remove_punc(token); if((strlen(token) <= 3) || isdigit(*token)) { continue; } if(*token != '\0') { head = add_word(head, filenames, token, fname); } } } return head; }
int main(int argc, char *argv[]) { const char* INPUT_PATH = argv[1]; const char* OUTPUT_PATH = argv[2]; const char* QUIET = argv[3]; /* Check arugments are present */ if (!INPUT_PATH || !OUTPUT_PATH) { printf(ANSI_RED "WordCounter: Arguments Missing.\n" ANSI_RESET); printf("Usage: WordCounter input-path output-path [-q]\n"); exit(EXIT_FAILURE); } /* Try and open file */ FILE *fp = fopen(argv[1], "r"); if (fp == NULL) { printf(ANSI_RED "Failed to open file: %s\n" ANSI_RESET, argv[1]); exit(EXIT_FAILURE); } /* Iterate text file adding words */ WordList *wl = WordList_new(); while(!feof(fp)){ char *word = malloc(sizeof(char)); fscanf(fp, "%s", word); remove_punc(word); if(!wl->increment(wl, word)){ wl->add(wl, word); } } fclose(fp); wl->sort(wl, true); /* If not quiet then print the results */ if (!QUIET) { wl->print(wl); } wl->save(wl, argv[2]); /* Free memory */ for (int i=0; i<wl->used; i++) { free(wl->words[i].chars); } free(wl->words); free(wl); printf(ANSI_GREEN "Completed, %d words saved to '%s'\n" ANSI_RESET, wl->used, argv[2]); }