Jump to content
  • Advertisement
Sign in to follow this  
MrSaturnFan

Reading from a file

This topic is 4593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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

Share this post


Link to post
Share on other sites
For which string function does it say in the linux man pages "Never use this function."? Isn't it strtok?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!