-
Dynamic fgets
Hi all,
I want to change following function to a "dynamic" fgets for string with unknown length and without any use of a buffer.Could someone give me an example with realloc please?
Code:
...
int ReadInputToTree(
WORD **DestTree,
size_t *Treecount,
FILE *Input)
{
int Status = SUCCESS;
char Buf[8192] = {0};
char *Word = NULL;
while(fgets(Buf, sizeof Buf, Input) != NULL)
{
Word = strtok(Buf, NONALPHA);
while(Status == SUCCESS&& Word != NULL)
{
Status = AddToTree(DestTree, Treecount, Word);
if(Status == SUCCESS)
{
Word = strtok(NULL, NONALPHA);
}
}
}
return (Status);
}
...
-
The idea is that fgets stores the newline from the read if there is space for it, so if you didn't detect the newline in the fgets read, you know that there's possibly still more to read (or maybe the newline is the only thing next, but that's fine).