我正在尝试构建一个二叉树,它将按字母顺序保存文件中的单词,并计算文件中单词的出现次数。然后稍后我必须能够替换原始文本文件中的单词。现在我只是试图设置我的二叉树并在那里获取单词。字符串标记有效,它将打印每行的单词和标点符号。我还必须将标点符号存储在字符数组中并计算其出现次数。我的插入功能有问题,但我不确定我做错了什么。我遇到了分段错误。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
Name: Marcus Lorenzana
*/
//binary tree struct to hold left and right node
//as well as the word and number of occurrences
typedef struct node
{
char* word;
int count;
struct node *left;
struct node *right;
}
node;
node *insert(node *item, char *word);
char* readFile(char* filename);
int main()
{
FILE *fin;
char *word;
fin = fopen("data.txt", "r");
char* filecontents = readFile("data.txt");
//create dictionary node
node *dictionary;
dictionary = NULL;
//read words and punctuation in from the text file
word = strtok (filecontents, " \n");
int i = 0;
while (word != NULL)
{
printf("%s\n",word);
insert(dictionary,word);
printf("%s",dictionary->word);
word = strtok (NULL, " \n");
i++;
}
return 0;
}
//not sure if works
node *insert(node *item, char *word)
{
if(item==NULL)
{
item= (node*) malloc(sizeof(node));
strcpy(item->word, word);
item->left=NULL;
item->right=NULL;
item->count++;
}
else
{
if(strcmp(word, item->word)<0)
{
item->left=insert(item->left, word);
item->count++;
}
else if(strcmp(word, item->word)>0)
{
item->right=insert(item->right, word);
item->count++;
}
else
{
item->count++;
}
}
return item;
}
char* readFile(char* filename)
{
FILE* file = fopen(filename,"r");
if(file == NULL)
{
return NULL;
}
fseek(file, 0, SEEK_END);
long int size = ftell(file);
rewind(file);
char* content = calloc(size + 1, 1);
fread(content,1,size,file);
return content;
}