Obtaining user input can be done in many surprisingly different ways. This code is somewhere in the middle: safer than gets(text)
or scanf("%s", text)
, but not bulletproof. It is meant to be a simple but relatively safe demonstration.
The function mygetline
reads user input from the stdin
into a string using fgets
. Then it attempts to remove a trailing newline that fgets
may leave in the string. It returns a pointer to the destination string.
See also Safe Version of gets() and Read a Line of Text from the User, Discard Excess Characters.