#define FILENAME "sample.txt"
#define DELIM " ~`!@#$%^&*()_+-={}[]|\\\"\':;<>?,./0123456789\n\r\t"
FILE* input;
char line[1500];
char* str;
int main(int argc, char** argv)
{
char finish;
LinkedList list;
FILE* input;
char line[1500];
char* str;
initializeLL(&list);
if((input = fopen(FILENAME,"r")) == NULL)
printf("Error opening file");
while((fgets(line,sizeof(line),input))!= NULL)
{
str = strtok(line,DELIM);
if(str!=NULL)
list = addWord(list,str);
while((str = strtok(NULL,DELIM)) != NULL)
{
if(str!=NULL)
list = addWord(list,str);
}
}
printLL(list);
scanf("%c",&finish);
fclose(input);
return 0;
}
Reading from a file
I'll bare you with the extreme details of my school assignment. But essentially what I must do is take a text file. (in paragraphs)Take out all the words and put them into a linked list without having duplicates. Now my linked list works and adds the word AND checks for multiples (it also ignores case).
My problem is is when i'm reading in it seems. I've debugged it myself (using scanf statements and printing the list after each word is added). It prints properly up until it reaches the end of the line. Then the list gets REALLY screwy, starts adding blank spaces and halves of words. I'll include the important parts of the code:
I have reason to believe it's something to do with newline characters. Or maybe i'm not understanding how the fgets function works or the strtok function.
And btw, dev-c++'s debugger seems to be broken. So I'm hoping someone can see something I'm missing.
I think that delimiter list is too long. Start simple and then add complexity. For example, start with a single space, " ", then add other punctuation as needed.
For reference: fgets, strtok
For reference: fgets, strtok
For which string function does it say in the linux man pages "Never use this function."? Isn't it strtok?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement