No, if you want cross-platform, do it properly:
fgets (buf, sizeof (buf), stdin);
if (strlen (buf) > 0)
if (buf[strlen (buf) - 1] == '\n')
buf[strlen (buf) - 1] = '\0';
You may run in to problems when reading Windows-created files on UNIX (for example) but that will work for its intended purpose, modifying standard input.
If you're worried about multiple calls to strlen
:
fgets (buf, sizeof (buf), stdin);
{
size_t len = strlen (buf);
if (len > 0)
if (buf[len - 1] == '\n')
buf[len - 1] = '\0';
}
By the way, if you're looking for a robust line input solution, which does newline removal, buffer overflow detection, excess line clean-up and uses only standard C, you should take a look at this answer.