?
This is a discussion on fgets - It will always return [string]\n ? within the
C Programming forums, part of the General Programming Boards category; Sorry. Im still a bit upset over input validation.
Consider the following:
Code:
fgets(BUF,10,stdin);
BUF is supposed to take in ...
-
fgets - It will always return [string]\n\0 ?
Sorry. Im still a bit upset over input validation.
Consider the following:
Code:
fgets(BUF,10,stdin);
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'.
Defination of fgets:
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.
So there are 3 conditions to monitor:
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?
Condition 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 ?
Im getting confused. Can someone enlighten please? Thanks
If I'm able to turn back time, I would learn C as my first language.
-
try putting this into a program, and experimenting with it:
Code:
fgets(buffer, 10, stdin);
printf("%s", buffer); you'll notice that you're right on all conditions.
however, on your first condition, you say the user keys in exactly num-1 characters, but thats not correct seeing as how the \n is a character. So, you're actually entering 10 characters there, and fgets will take off the last character in order to put its null character in
-
and the hat of mystery
> What happened to the \n then?
> The remaining chars will be stored in the buffer?
You'll get them on the next fgets() call.
Basically, if you don't find a \n, then expect a long line and deal with it accordingly.
-
Oh ok. Thanks! I shall play with it again.
But about backspaces '\b'?
Are \b being taken in also? And what about tabs '\t' ?
Are they considered to be one character also?
If I'm able to turn back time, I would learn C as my first language.
-
Registered Luser

Originally Posted by
stevong Oh ok. Thanks! I shall play with it again.
But about backspaces '\b'?
Are \b being taken in also? And what about tabs '\t' ?
Are they considered to be one character also?
Yes, yes, yes, yes, respectively.
-

Originally Posted by
Salem > What happened to the \n then?
> The remaining chars will be stored in the buffer?
You'll get them on the next fgets() call.
Basically, if you don't find a \n, then expect a long line and deal with it accordingly.
oh ok..Hmm...I just wrote and tested this and it seems fine in my outputs.
it managed to clear unwanted buffer (the extra buffer that exceeded the desire char length) and the irritating \n at end of the input (if any)
Is this foolproof enough?
Please advise. 
Code:
fgets(buf,10,stdin);
if(buf[strlen(buf)-1]!='\n')
while((ch=getchar())!='\n' && ch!=EOF);
else
buf[strlen(buf)-1]='\0';
If I'm able to turn back time, I would learn C as my first language.
-
and the hat of mystery
Yes, if you want to do that, then that would be ok
-
Just Lurking

Originally Posted by
stevong Code:
fgets(buf,10,stdin);
if(buf[strlen(buf)-1]!='\n')
while((ch=getchar())!='\n' && ch!=EOF);
else
buf[strlen(buf)-1]='\0'; You don't really need to walk the string twice. You could capture the result of strlen in a variable and use it again, like in the foo, here.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Popular pages Recent additions
Similar Threads
-
By rwmarsh in forum Game Programming
Replies: 4
Last Post: 09-24-2006, 10:00 PM
-
By linkofazeroth in forum Game Programming
Replies: 4
Last Post: 09-13-2005, 10:17 AM
-
By Micko in forum C Programming
Replies: 8
Last Post: 10-04-2004, 02:04 PM
-
By CodeMonkey in forum C++ Programming
Replies: 13
Last Post: 07-20-2003, 11:20 PM
-
By Unregistered in forum Windows Programming
Replies: 4
Last Post: 02-14-2002, 09:01 PM