Mr. Pallini gave you some very important advice I thought I would elaborate on. He recommended using
fgets
and I agree completely. It in conjunction with a constant is the best option. Here's an example :
const int BufferSize = 1000;
char buffer[ BufferSize + 1 ] = { 0 }; fgets( buffer, BufferSize, stdin );
The important things there are the +1 in the declaration of buffer to allow room for the null character, initializing the buffer to 0 to force the trailing null to be there, and then using
fgets
without the +1 so the null character remains.
This same tactic with the +1 also works with
strncpy
,
strncat
,
snprintf
, and other functions that take a buffer size parameter. You need to remember to initialize the buffer to zeros to ensure the trailing null is there though because in debug mode buffers are not always initialized to zero.