i wrote this function to scan a word from a file:
char * fgetword(FILE *f)
{
char *word,*tmp,c;
int i = 0;
word = (char*)calloc(MAXLENGHT,sizeof(char));
tmp = word;
while((c = fgetc(f)) <=32 && c!= EOF)
;
if (c == EOF)
{
free(word);
word = NULL;
}
else
*tmp++ = c;
while((c = fgetc(f)) > 32 && c != EOF && i++ < MAXLENGHT)
*tmp++ = c;
*tmp = '\0';
return word;
}
i using it to parse a file with hundredthousands lines of text. i use it like this:
while ((word = fgetword(f1)) != NULL && strcmp(word,"Testfallname:") != 0 )
{;}
if (word == NULL)
return 0;
fputs("Testname: ",f2);
free(word);
this whole thing is in one big while not eof loop.
after scanning and working correct for about 5 to 10 minutes, it crashes.
is there a fault in my free()? is there any good way of detecting memory leaks....?