I want to read strings from a text file (one string/word per line) and then arrange them by size.
This is my code:
void readDic(char* file)
{
FILE* fr;
fr=fopen(file, "rt"); // opening the text file
char line[MAX_LINE_SIZE];
char* word;
while(fgets(line, MAX_LINE_SIZE, fr)!=NULL)
{
if(line[0]!='\n')
{
word = strtok(line, "\n"); //remove the newline from the string
// do stuff with word
}
}
fclose(fr);
}
Although this code code runs, every string I read, except the last one, comes with a size of +1 than the one in the file.
For example, strlen of the string "hello" returns 6 if its anywhere except the last lineof the file. If it is in the last line of the file strlen returns 5.
Am I doing something wrong?