I use Codeblocks on my computer and also checked it with dev++ and both give the same correct results when I give test.txt as input file and stop.txt.https://pastebin.com/Z8CPnJNd (test.txt) and https://pastebin.com/qhLU9QV3 (stop.txt)
However when I use CentOs OS with gcc it doesn't remove the stop words from the structure list. For example string of
must be removed.
I run the terminal, I used command gcc main.c -o main -std=c99
and then ./main
I don't know if any const is causing the problem but I already checked.
This is the part that should be executed
typedef struct _word { char *s; /* the word */ int count; /* number of times word occurs */ int *line_numbers; // Array of line numbers int num_line_numbers; // Size of the array of line numbers char *fileno;} word;// Creating a struct to hold the data. I find it's easiertypedef struct { word *words; // The array of word structs int num_words; // The size of the array} word_list;char *strlower(char *s) { for (size_t i = 0; s[i]; i++) { s[i] = (char)tolower((unsigned char)s[i]); } return s;} int remove_word(word_list *words, const char *word_to_delete) { int found = 0; for (int i = 0; i < words->num_words; i++) { if (!strcmp(words->words[i].s, word_to_delete)) { // Calc number of words after found word int number_of_words_to_right = words->num_words - i - 1; // Free mem free(words->words[i].s); free(words->words[i].line_numbers); free(words->words[i].fileno); if (--words->num_words == 0) { free(words->words); words->words = NULL; } else { // Copy remaining words if any memcpy(&words->words[i], &words->words[i + 1], sizeof(word) * number_of_words_to_right); // Resize the array (technically not required) word *tmp = realloc(words->words, sizeof(word) * words->num_words); if (tmp != NULL) words->words = tmp; } found++; i--; // restart from the same index in the loop } } return found; }main:int main() { int option = 1; word_list *words = calloc(sizeof(word_list), 1); if (words == NULL) { fprintf(stderr, "cannot allocate memory\n"); return 1; } read_file(words, "test.txt"); if (option != 0) { char s[1000]; FILE *file = fopen("stop.txt", "r"); /* should check the result */ if (file == NULL) { fprintf(stderr, "cannot open %s\n", "stop.txt"); } else { while (fgets(s, sizeof(s), file)) { char *word = strtok(s, " \n"); while (word != NULL) { remove_word(words, strlower(word)); word = strtok(NULL, " \n"); } } fclose(file); } } printlist(words); for (int i = 0; i < words->num_words; i++) { free(words->words[i].s); free(words->words[i].line_numbers); free(words->words[i].fileno); } free(words->words); free(words); return 0;}Code:https://pastebin.com/aWWHyeyi