Sorry. Im still a bit upset over input validation.
Consider the following:
BUF is supposed to take in 10 chars. But only 9 chars will be readable by us. Since the last char will always be a '\0'.Code:fgets(BUF,10,stdin);
Defination of fgets:
So there are 3 conditions to monitor:Reads characters from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first.
A newline character ends reading but is considered a valid character and included in the new string.
A null character is always appended at the end of the resulting string.
1. A user keys in exactly (num-1) chars.
2. A user keys in more than (num-1) chars.
3. A user keys in less than (num.1) chars.
Condition 1:
If a user keys in the following:
TAEKWONDO
and hits a carriage return; what is actually being taken into the system is:
TAEKWONDO\n\0
So BUF will be TAEKWONDO\0 ? Since BUF has already read (num-1) chars? What happened to the \n then?Condition 2:
But if a user keys in the following:
TAEKWONDOTAEKWONDOTAEKWONDO
and hits a carriage return; what is being captured by the system is:
TAEKWONDOTAEKWONDOTAEKWONDO\n\0
So BUF will be TAEKWONDO\0 also? The remaining chars will be stored in the buffer?Im getting confused. Can someone enlighten please? ThanksCondition 3:
If a user keys in the following:
HELLO
and hits a carriage return; what is being captured by the system is:
HELLO\n\0
BUF will be exactly HELLO\n\0 ?