home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / os / msdos / programm / 8081 < prev    next >
Encoding:
Text File  |  1992-07-25  |  1.5 KB  |  43 lines

  1. Newsgroups: comp.os.msdos.programmer
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!uunet.ca!canrem!telly!druid!darcy
  3. From: darcy@druid.uucp (D'Arcy J.M. Cain)
  4. Subject: Re: Borland C++ 3.0 scanf()/gets() bug.
  5. Message-ID: <1992Jul25.123910.4488@druid.uucp>
  6. Date: Sat, 25 Jul 1992 12:39:10 GMT
  7. References: <1992Jul23.181820.29495@unixg.ubc.ca>
  8. Organization: D'Arcy Cain Consulting
  9. Lines: 32
  10.  
  11. danc@loon.geog.ubc.ca (Dan Ciarniello) writes:
  12. >I've come across what I believe is a bug in Borland C++.  The following 
  13. >program fragment does not work as one would expect.
  14. >        scanf("%s", var1);
  15. >    gets(var2);
  16.  
  17. Nope.  You found a bug in your program.  Actually there are two bugs
  18. in the above code.  To find the bugs I mean add the following flags
  19. to your compile command:
  20.  
  21.     -Dscanf=NEVER_USE_SCANF -Dgets=NEVER_USE_GETS
  22.  
  23. If you do this in all of your programs you will save yourself much grief.
  24. The call to scanf is doing exactly what it is supposed to do.  The way
  25. to do what you want is:
  26.  
  27.     fgets(buffer, sizeof(buffer), stdin);
  28.     sscanf("%s", var1);
  29.     fgets(var2, sizeof(var2), stdin);
  30.  
  31. CAUTION:  The above assumes that buffer and var2 are character arrays,
  32. not pointers.  Replace sizeof to something appropriate if that is not
  33. true.
  34.  
  35. BTW the fgets over gets issue is to cover the times that someone leaves
  36. a book resting on the keyboard while they answer the phone.
  37.  
  38. -- 
  39. D'Arcy J.M. Cain (darcy@druid.com)  |
  40. D'Arcy Cain Consulting              |   There's no government
  41. Toronto, Ontario, Canada            |   like no government!
  42. +1 416 424 2871          DoD#0082   |
  43.