TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: DAVID NOON
from: DAVID VAN HOOSE
date: 1998-04-02 06:27:00
subject: Reading Input From Stdin

-> DH>char name[41];
-> DH>gets(name);
-> DH>name[41]='\0';
-> DH>printf("%s",name);
->
-> DH>My bad.
->
-> It still is, I'm afraid.
->
-> An array of size 41 has subscripts 0 thru 40, so the 3rd line has a
-> subscript out of bounds.
->
-> Moreover, the gets() function does not limit the transfer to the size
-> of the supplied buffer, since it does not know that size. The use of
-> fgets() instead is much safer.
1: fgets() is for use with files.
2: It works, and works perfectly if you ever bothered to try it.
3: It counts 0 thru 41 standardly. using cin.get() it uses 0 thru 40
   because it already accounts for the NULL on the end.
Ok?  Try it.  You'll see that I am right. I use that all the time.
-Dave
1:3612/333
333:1/1
davidvh@ibm.net
--- PCBoard (R) v15.3/100
---------------
* Origin: Destiny BBS: LightBar Central (1:3612/333)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.