delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/03/10/08:49:05

From: Thomas Demmer <demmer AT LSTM DOT Ruhr-UNI-Bochum DOT De>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: djgpp bug?
Date: Mon, 10 Mar 1997 12:49:22 +0100
Organization: Lehrstuhl fuer Stroemungsmechanik
Lines: 79
Message-ID: <3323F542.41C6@LSTM.Ruhr-UNI-Bochum.De>
References: <5g0mss$ivc AT nntp DOT novia DOT net> <5g0nmc$k4k AT nntp DOT novia DOT net>
NNTP-Posting-Host: bvb.lstm.ruhr-uni-bochum.de
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Alaric Dailey wrote:
> 
>  the following file compiles and runs perfectly under linux gcc and Turbo C
>  for dos however when you compile it under djgpp it skips it skips entering
>  the keyword.  This is an example program from a book that I have modified
>  because it didn't work correctly under gcc. Imagine my surprise when I
>  found out that it still doesn't work under djgpp. I think that perhaps
>  fflush() isn't working properly. maybe I am wrong could someone tell me
>  what is going on with the following code?

[...]

>  /**************************************************************
>         FUNCTION NAME: get_definition
>         PURPOSE: uses gets to receive input of a definition
>                 from the user.
>         INPUT: none.
>         OUTPUT: a dict_entry structure
>         AUTHOR: MCD
>  **************************************************************/
>  dict_entry get_definition(void)
>  {
>         int i;
>         dict_entry out_entry;
>         dict_entry *out_entryp = &out_entry;
>         printf("\n Enter keyword: ");
>         /* since out_entry is a localstructure variable and
>            not a structure pointer we access
>            its members using the dot operator. */
> 
>         fgets(out_entry.keyword,80,stdin);
>  /*     scanf("%s",out_entryp->keyword);*/
>         fflush(stdin);  /*this is so the \n gets flushed
>                           out of the buffer */
> 
>         printf("\n Enter definition of up to %d lines.", SENTENCE_MAX);
>         for(i=0; i < SENTENCE_MAX; i++)
>         {
>                 printf("\n Line %d: ",i+1);
>                 /*scanf("%s",out_entryp->sentence[i]);*/
>                 fgets(out_entry.sentence[i],80,stdin);
>         }
>         return(out_entry);
>  }

Here is where you most probably fail:
fgets() gives you an input line+\n\0
So if you have the keyword "foo", and your user enters 
"foo"<return>,
out_entry.keyword really is "foo\n". 

For a _real_ fool proof way of getting input lines of unknown
length, check the FAQ-sources. Somewhere in there Eli has put
a routine that reads into a buffer, checks if is terminated
with \n, and reallocates and reads on, if not.

BTW, fflush(stdin) _may_ be a bad idea. I think I remember
flushing an input stream is unspecified in ANSI. Any wizard
knowing this for sure?

And, also, why don't you just use bison & flex. That's exactly
what they are made for.
[...]

-- 
Ciao
Tom

*************************************************************
* Thomas Demmer                                             *
* Lehrstuhl fuer Stroemungsmechanik                         *
* Ruhr-Uni-Bochum                                           *
* Universitaetsstr. 150                                     *
* D-44780  Bochum                                           *
* Tel: +49 234 700 6434                                     *
* Fax: +49 234 709 4162                                     *
* Voice/Fax Box: +49 2561 91371 2056                        *
* http://www.lstm.ruhr-uni-bochum.de/~demmer                *
*************************************************************

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019