मैं strtok() के साथ फ़ाइल में शब्दों की संख्या गिनने की कोशिश कर रहा हूं।अप्रत्याशित strtok() व्यवहार
/*
* code.c
*
* WHAT
* Use strtok() to count the number of words in a file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRMAX 128
int main() {
/* Declarations */
FILE* fptr;
int iCntr = 0;
char sLine[STRMAX];
char* cPToken;
/* Read file */
/* Error handler */
if ((fptr = fopen("/home/ubuntu/Dropbox/Unief/C/H18/Opdr01/Debug/test.txt", "r")) == NULL) {
printf("Couldn't read test.txt.\n");
exit(0);
} else {
while (fgets(sLine, STRMAX-1, fptr) != NULL) { /* Read line */
while ((cPToken = strtok(sLine, ".,; !?\r\n")) != NULL) { /* Split into words */
iCntr++;
}
}
printf("Number of words: %d\n", iCntr);
}
/* Always clean up your mess */
fclose(fptr);
return 0;
}
यह एक अनंत लूप का कारण बनता है। क्यूं कर?
माध्यम से उपयोग करने के 'fgets (sLine, STRMAX, fptr)' 'नहीं STRMAX-1' – user102008