Sto leggendo dal mio dizionario e stampando la parola + la lunghezza della parola a scopo di test.strlen che non fornisce la lunghezza corretta della stringa C
Io uso strlen per ottenere la lunghezza della stringa. Tuttavia, i numeri che ho ricevuto non sono corretti. Credo che strlen non contenga il carattere \ 0.
Sto leggendo le prime 10 parole nel dizionario. Il mio risultato atteso dovrebbe essere:
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
Ma questo è quello che ho ottenuto (Si noti come la L:. 'S sono su un'altra linea Penso che questo è dove il problema è):
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
Sotto è il mio codice:
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++;
}
Il risultato di 'fgets()' include spesso ''\ n''. Per tagliare, vedi http://stackoverflow.com/a/28462221/2410359 BTW, domanda ben formata, anche se certamente un duplicato. – chux
"Credo che' strlen' non contenga il carattere ''\ 0''." - No, non è così, e non dovrebbe. (Qualsiasi riferimento alla funzione 'strlen', incluso' man strlen' se il tuo sistema ha pagine man, te lo direbbe.) –