Sign in to follow this  
MrSaturnFan

Reading from a file

Recommended Posts

MrSaturnFan    122
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:
#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;
}



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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this