🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.msdos.programmer
Article #488

Re: Borland C++ 3.0 scanf()/gets() bug.

#488
From: D'Arcy J.M. Cain
Date: Sat, 25 Jul 1992 12:39
32 lines
1619 bytes
danc@loon.geog.ubc.ca (Dan Ciarniello) writes:
>I've come across what I believe is a bug in Borland C++.  The following 
>program fragment does not work as one would expect.
>        scanf("%s", var1);
>	gets(var2);

Nope.  You found a bug in your program.  Actually there are two bugs
in the above code.  To find the bugs I mean add the following flags
to your compile command:

    -Dscanf=NEVER_USE_SCANF -Dgets=NEVER_USE_GETS

If you do this in all of your programs you will save yourself much grief.
The call to scanf is doing exactly what it is supposed to do.  The way
to do what you want is:

    fgets(buffer, sizeof(buffer), stdin);
    sscanf("%s", var1);
    fgets(var2, sizeof(var2), stdin);

CAUTION:  The above assumes that buffer and var2 are character arrays,
not pointers.  Replace sizeof to something appropriate if that is not
true.

BTW the fgets over gets issue is to cover the times that someone leaves
a book resting on the keyboard while they answer the phone.

-- 
D'Arcy J.M. Cain (darcy@druid.com)  |
D'Arcy Cain Consulting              |   There's no government
Toronto, Ontario, Canada            |   like no government!
+1 416 424 2871          DoD#0082   |
Technical Details
Message-ID: <1992Jul25.123910.4488@druid.uucp>
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!uunet.ca!canrem!telly!druid!darcy
References: <1992Jul23.181820.29495@unixg.ubc.ca>

Show Full Headers
Complete Article Headers
Newsgroups: comp.os.msdos.programmer
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!uunet.ca!canrem!telly!druid!darcy
From: darcy@druid.uucp (D'Arcy J.M. Cain)
Subject: Re: Borland C++ 3.0 scanf()/gets() bug.
Message-ID: <1992Jul25.123910.4488@druid.uucp>
Date: Sat, 25 Jul 1992 12:39:10 GMT
References: <1992Jul23.181820.29495@unixg.ubc.ca>
Organization: D'Arcy Cain Consulting
Lines: 32