Using fgets
to read in a line is simpler and more robust:
if (!fgets(buff, 28, stdin))
{
// reading failed, do appropriate error handling
// we're just exiting here
exit(EXIT_FAILURE);
}
// We have successfully read in a line, or at least the first 27
// characters of the line. Check whether a full line was read,
// if it was, whether the line was empty
size_t l = strlen(buff); // <string.h> must be included
if (buff[l-1] == '
')
{
// a full line was read, remove trailing newline unless
// the line was empty
if (l > 1)
{
buff[l-1] = 0;
}
}
else
{
// the input was too long, what now?
// leave the remaining input for the next iteration or
// empty the input buffer?
}
printf("%s
",buff);
It doesn't work with scanf("%s",buff)
because most scanf
conversions ignore leading white space:
Input white-space characters (as specified by the isspace
function) are skipped, unless
the specification includes a [
, c
, or n
specifier.
So if the user inputs an empty line, scanf
ignores that input unless its format is one of the exceptional.
You can use scanf
with a character set format instead,
scanf("%27[^
]%*c", buff);
to read all characters until a newline (but limited to 28 - 1
here to avoid buffer overruns), and then consume a newline without storing it (the *
in the %*c
conversion specifier suppresses assignment), that would handle non-empty lines consisting entirely of whitespace, which the %s
conversion would not. But if the first character of the input is a newline, the %27[^
]
conversion fails (thanks to chux for drawing attention to that), the newline is left in the input buffer, and subsequent scans with that format would also fail if the newline isn't removed from the input buffer.
A somewhat robust (but ugly; and not dealing with too long input) loop using scanf
would, as far as I can see, need to check for a newline before scanning, e.g.
for(int ct = 0; ct < i; ++ct)
{
int ch = getchar();
if (ch == EOF)
{
// something bad happened; we quit
exit(EXIT_FAILURE);
}
if (ch == '
')
{
// we had an empty line
printf("
");
}
else
{
// The first character was not a newline, scanning
// with the character set format would have succeeded.
// But we don't know what comes next, so we put the
// character back first.
// Although one character of pushback is guaranteed,
if (ungetc(ch,stdin) == EOF)
{
// pushback failed
exit(EXIT_FAILURE);
}
scanf("%27[^
]%*c",buff);
printf("%s
",buff);
}
}
Use fgets
, really. It's better.