string - How to limit scanf function in C to print error when input is too long? -
i want limit scanf function when enter example char* array <string...> has more 30 characters, not , output error.
i got hint use [^n] or don't understand how it? know can use scanf("%30s"..) don't want input valid , error.
any great.
if must use scanf believe best can use width specifier like: "%31s", you've mentioned, use strlen check length of input, , discard string , report error if input longer limit.
or possibly skip strlen additionally using %n in format string, e.g. "%31s%n".
a format string using %[^\n] in place of %s instructs function continue reading until newline, consuming other whitespace characters along way. useful if want allow input include whitespace characters.
review docs scanf (here's copy of man page).
Comments
Post a Comment