Why do I get an assertion failure?

My guess is that the file is failing to open, and you’re still passing it to fgets. Your if(rd==NULL) doesn’t stop execution of the fgets if it’s null, it just prints out a message and continues with execution.

Some very basic errorr handling:

const char* frd = "word-list.txt";

FILE *rd=fopen(frd,"r");
if(rd==NULL) {
    std::cout<<"Coudn't open file"<<endl;
    return 1;
}

char readLine[100]; 
while(fgets(readLine, 100, rd) != NULL)
{     
    readLine[strlen(readLine) - 1] = '\0'; 
    char *token = NULL; 
    token = strtok(readLine, " ,"); 
    insert(readLine);
}

Leave a Comment