我有一个文件“data.txt”,其中包含以下内容:http: //pastebin.com/FY9ZTQX6
我试图在“<”之前和之后得到这个词。左边是旧词,右边是新词。这是我到目前为止所拥有的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/*
Name: Marcus Lorenzana
Assignment: Final
*/
//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;
//,.?!:;-
int punctuation[7];
void insert(node ** dictionary, node * entry);
char* readFile(char* filename);
void printDictionary(node * tree);
void toLower(char** word);
void getReplacementWords(char *filecontents, char **newWord, char **oldWord) ;
int main()
{
char *word;
char* filecontents = readFile("data.txt");
char* oldWord;
char* newWord;
//create dictionary node
node *dictionary;
node *entry;
//read words and punctuation in from the text file
word = strtok (filecontents, " \n");
dictionary = NULL;
while (word != NULL)
{
//word = strlwr(word);
entry = (node *) malloc(sizeof(node));
entry->left = entry->right = NULL;
entry->word = malloc(sizeof(char)*(strlen(word)+1));
entry->word = word;
insert(&dictionary,entry);
word = strtok (NULL, " \n");
}
//printDictionary(dictionary);
filecontents = readFile("data.txt");
getReplacementWords(filecontents,&newWord,&oldWord);
return 0;
}
void insert(node ** dictionary, node * entry)
{
if(!(*dictionary))
{
*dictionary = entry;
entry->count=1;
return;
}
int result = strcmp(entry->word,(*dictionary)->word);
if(result<0){
insert(&(*dictionary)->left, entry);
entry->count++;
}
else if(result>0){
insert(&(*dictionary)->right, entry);
entry->count++;
} else {
entry->count++;
}
}
//put file contents in string for strtok
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;
}
void printDictionary(node * dictionary)
{
if(dictionary->left) {
printDictionary(dictionary->left);
}
printf("%s\n",dictionary->word);
if(dictionary->right) {
printDictionary(dictionary->right);
}
}
void getReplacementWords(char *filecontents, char **newWord, char **oldWord) {
char *word;
word = strtok (filecontents, " \n");
while (word != NULL)
{
printf("\n%s",word);
int result = strcmp(word,"<");
if (result == 0) {
printf("\nFound replacement identifier");
}
word = strtok (NULL, " \n");
}
}