Is it possible to create a dynamic char array on the fly directly from stdin? I do not want to create a fixed length array before hand and then copy contents of it into a malloc created array.
Code:
//[1]
char line[MAX1];
gets(line);
//[2]
char line[MAX1];
fgets(line,MAX1-1,stdin);
//[3]
char *str = (char *)malloc(sizeof(char)*(strlen(line)));
I could do either [1](buffer overflow problem) or [2] and then goto [3]. But both will have a problem if the input is more than the size MAX1(use defined).
Is it possible to do something of the effect of readLine() method of BufferedReader class in Java or the Console.readLine in .NET? Is it possible to peek into stdin and see the size of the input and then creat an array of the same exact size?