Recently I need a way to read a line from stdin, but it was quite important that I read the entire line (as some of the inputs were quite large). So I decided to have a go at writing a function, that reads, character by character input from stdin until it reaches either EOF or '\n'. It would then add the character that it read onto the end of a char array (malloc'ed) which is passed in as the first parameter. My code, however does not work (and even if it did it would have a good few problems), but this was the best that I could come up with:
Code:
int getLine (char *charPtr)
{
char c;
int i;
int totalAlloc = 32;
charPtr = malloc(32);
for (i = 0; (c = fgetc(stdin)) != EOF || c != '\n'; i++)
{
if ((i + 1) > totalAlloc)
{
realloc(charPtr, totalAlloc + 32);
totalAlloc += 32;
}
charPtr[i] = c;
}
i += 2;
realloc(charPtr, i);
charPtr[i - 1] = '\0';
return i;
}
The function assumes that the memory passed to it has been freed first. The idea is to reach each char from stdin and add it onto the end of the array, if the array is out of space then allocate some more. I am not sure why it does not work (compiles fine...) but the only problem that I can see is with realloc. If the memory that it allocates has a different address then I am in trouble, as I (do not think) that I am able to change the memory that the pointer parameter points to (by that I mean assign charPtr to a different memory address).
Can anyone help me?