The GCC documentation says that:
Standard C has functions to do this, but they aren't very safe: null characters and even (for gets
) long lines can confuse them. So the GNU library provides the nonstandard getline
function that makes it easy to read lines reliably.
and that
[getline
] is a GNU extension, but it is the recommended way to read lines from a stream. The alternative standard functions are unreliable.
So if you're using GCC, I'd say you should use getline
. If you're not using GCC, you should see if your compiler offers a similar feature. If it doesn't — or if you really prefer to use something standard — then you need to read one character at a time.
I don't know how to read one character at a time without having the user press enter after each character.
Yes, you do. The user enters a sequence of characters, and then presses Enter. Your first fgetc
call will block until the user presses Enter, but after that, subsequent fgetc
calls will return immediately, up until you read the newline. (Then it will block again, until the user presses Enter again.) Reading "one character at a time" doesn't mean that you have to read each character before the user types the next one; it just means that, once the user is done typing a line, you read that line one character at a time.