问题
I am reading from my dictionary and printing out the word + the length of the word for testing purposes.
I use strlen to get the length of the string. However, the numbers I got are not correct. I believe strlen doesn't count the \0 character.
I am reading the first 10 words in the dictionary. My expected output should be:
W:A L:1
W:A's L:3
W:AA's L:4
W:AB's L:4
W:ABM's L:5
W:AC's L:4
W:ACTH's L:6
W:AI's L:3
W:AIDS's L:6
W:AM's L:4
But this is what I got (Notice how the L:'s are on another line. I think this is where the problem is):
W:A
L:2
W:A's
L:4
W:AA's
L:5
W:AB's
L:5
W:ABM's
L:6
W:AC's
L:5
W:ACTH's
L:7
W:AI's
L:5
W:AIDS's
L:7
W:AM's
L:5
Below is my code:
FILE* dict = fopen("/usr/share/dict/words", "r"); //open the dictionary for read-only access
if(dict == NULL) {
return;
}
int i;
i = 0;
// Read each line of the file, and insert the word in hash table
char word[128];
while(i < 10 && fgets(word, sizeof(word), dict) != NULL) {
printf("W:%s L:%d\n", word, (int)strlen(word));
i++;
}
回答1:
fgets() reads in the newline into the buffer if there's enough space. As a result, you see the newline printed when you print word
. From the fgets manual:
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.
(emphasis mine)
You have to trim it yourself:
while(i < 10 && fgets(word, sizeof(word), dict) != NULL) {
size_t len = strlen(word);
if ( len > 0 && word[len-1] == '\n' ) word[len] = '\0';
printf("W:%s L:%d\n", word, (int)strlen(word));
i++;
}
回答2:
The reason is because fgets pulls the newline character '\n' into your buffer word
each time, leading to a higher count by 1 each time.
来源:https://stackoverflow.com/questions/31371764/strlen-not-giving-correct-string-length-c