OK I know I've posted something like this before but I now have a slightly different problem.
I am reading in input to a buffer line by line (using fgets) and then putting the buffer into an array of strings. I only need the first 100 elements (MAXBUF) of the array and so I cut it off with a null byte if it exceeds this.
My problem is that next time it goes through the while loop, it doesn't prompt for input, but instead, the buffer is now equal to whatever was after the null byte I cut it off with.
Am I missing something silly or is this how fgets works? I need to just discard anything after the cutoff null byte but can't seem to do it.
Here's my code:
Code:
while( fgets( buf, MAXBUF, stdin ) != NULL && nel < MAX_ELEM )
{
if (strlen(buf) >= MAXBUF)
{
buf[MAXBUF] = '\0';
printf("Buffer exceeds limit, snipping...\n");
}
data[nel] = (char*)malloc( (strlen(buf) + 1) * sizeof(char) );
assert( data[nel] != NULL );
strcpy( data[nel], buf );
nel++; /* Count number of elements in array */
}