I want to make a program that accepts an arbitrary number of characters from user input, until "enter" is pressed, and stores them in a buffer.
Is there a way that to read characters from stdin
without extracting them, count the characters, then allocate a buffer of precise size, finally copy the characters to the buffer.
Basically I do NOT want the way of using getc
in loop and doubling the buffer size as it's running out.
EDIT: To make my intentions more clear, let me express my intuition. I imagine the stdin buffer the same as a file (which may or may not grow dynamically). So I should be able to seek to the end of it (representing the end of user input), counting the offset then rewind back. Something like
long const start = ftell(stdin);
fseek(stdin, 0, SEEK_END);
long const length = ftell(stdin) - start;
rewind(stdin);