The code below is giving me problems on Windows Mingw32 but not on WSL Ubuntu. The delimiter is (char)32 (space).
while (!feof(f)){
if(!fgets(line, LINE_LEN, f) || !*line || !strcmp(line, "\n")) continue;
word = strtok(line, &delim);
printf("xd\n");
while(word){
//printf("%s\n",word);s
add_item(h,word);
word = strtok(NULL, &delim);
wc++;
}
lc++;
}
I had tried debugging the code with CLion and the variable 'line' is correctly filled with given sentence that contain spaces, therefore strtok should not be returning null on the first iteration, yet it is. CLion Debug
Please replace that code with this:
// FILE *f presumed to be opened for reading
char line[ 1024 ];
int lc = 0, wc = 0;
while( fgets( line, sizeof line, f ) ) {
for( char *wp = line; ( wp = strtok( wp, " \n" ) ) != NULL; wp = NULL ) {
add_item( h, wp ); // unseen in this question.
wc++;
}
lc++;
}
Empty lines can be safely loaded into line
, and strtok()
will deal with them correctly (ie: find no words.)