I want my code to receive an endless stream of input until the user (or text file) returns EOF. The problem is that in both input cases the first character registered is some additional one. Case in point - the input was Applepie, why is the first character registered 'p'?
Code:
string = (char *) malloc(allocated);
while (c != EOF){
c = getchar();
if (counter >= allocated -1){
allocated *= 2;
new_buffer = realloc(string, allocated );
if (! new_buffer){
return (char *) string;
}
string = new_buffer;
}
/*store the char*/
string[counter] = c;
counter++;
/*printf("current char: %c\n", string[counter]);*/
}
string[counter] = '\0';
return (char*)string;
}