I wrote this program to read two files, separate the lines read into words and write the words, the number of times they were found on both files and the number of the where they came from. For some reason, my program is skipping the last word. Can someone help me find the problem? Thanks.
text1.txt
text2.txt
Code:
look how they shine for you
out.txt
Code:
look 2 1
at 1 1
the 1 1
stars 1 1
how 1 2
they 1 2
shine 1 2
for 1 2
Here's the program
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILES 2
int add_word(const char *, const int);
int word_exists(const char *);
void write_list(void);
struct Word {
char word[30];
int occurrences;
int document;
};
struct Node {
struct Word word;
struct Node *next;
};
struct Node *first_node = NULL;
int main(void)
{
int i;
FILE *in;
char line[BUFSIZ];
char *word = NULL;
char list[FILES][11] = {"text1.txt", "text2.txt"};
for(i = 0; i < FILES; ++i) {
in = fopen(list[i], "r");
if(in == NULL){
printf("unknown file: %s\n", list[i]);
return 0;
}
while(fgets(line, sizeof(line), in) != NULL) {
if(strchr(line, '\n') != NULL)
line[strlen(line) - 1] = '\0';
word = strtok(line, " ");
while(word != NULL) {
add_word(word, i + 1); /* i + 1 is the document's number */
word = strtok(NULL, " ");
}
}
fclose(in);
}
write_list();
return 0;
}
int add_word(const char *word2_add, const int doc_number)
{
struct Node *new_node;
struct Node *walker;
if(word_exists(word2_add) == 1)
return 0;
else {
new_node = (struct Node *)malloc(1 * sizeof(*new_node));
if(new_node == NULL){
printf("error allocating memory\n");
exit(1);
}
new_node->next = NULL;
strcpy(new_node->word.word, word2_add);
new_node->word.occurrences = 0;
new_node->word.occurrences++;
new_node->word.document = doc_number;
if(first_node == NULL)
first_node = new_node;
else {
walker = first_node;
while(walker->next != NULL)
walker = walker->next;
walker->next = new_node;
}
}
return 0;
}
int word_exists(const char *word)
{
struct Node *walker;
walker = first_node;
if(walker == NULL)
return 0;
else {
while(walker->next != NULL) {
if(strcmp(word, walker->word.word) == 0) {
walker->word.occurrences++;
return 1;
}
else
walker = walker->next;
}
}
return 0;
}
void write_list(void)
{
FILE *out;
struct Node *walker;
walker = first_node;
out = fopen("out.txt", "w");
if(out == NULL) {
printf("could not create out.txt\n");
getchar();
exit(1);
}
while(walker->next != NULL) {
fprintf(out, "%s \t %d \t %d\n", walker->word.word,
walker->word.occurrences, walker->word.document);
walker = walker->next;
}
fclose(out);
}
Thanks a lot.