delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2004/08/12/17:45:37

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
From: "John Hanley" <jdhanley AT telusplanet DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: program hanging
Date: Thu, 12 Aug 2004 15:37:28 -0600
Organization: University of Alberta
Lines: 154
Message-ID: <1092346578.112497@proxy2.srv.ualberta.ca>
References: <1092258885 DOT 100659 AT proxy2 DOT srv DOT ualberta DOT ca> <7105-Thu12Aug2004064431+0300-eliz AT gnu DOT org> <1092324277 DOT 615039 AT proxy2 DOT srv DOT ualberta DOT ca> <sv9nh0tndcdjgritie7gqkjth48290ftoe AT 4ax DOT com>
NNTP-Posting-Host: proxy2.srv.ualberta.ca
X-Trace: tabloid.srv.ualberta.ca 1092346580 8013 129.128.5.161 (12 Aug 2004 21:36:20 GMT)
X-Complaints-To: abuse AT ualberta DOT ca
NNTP-Posting-Date: Thu, 12 Aug 2004 21:36:20 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
Cache-Post-Path: proxy2.srv.ualberta.ca!unknown AT d199-126-23-72 DOT abhsia DOT telus DOT net
X-Cache: nntpcache 3.0.1 (see http://www.nntpcache.org/)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

> You do realize that this routine will eat not just spacing characters,
> but any and all special characters, including [+-.]?

Yes, I do.  Perhaps it's poorly named.  Only alphanumeric characters can be
used.

> If you just want to skip spacing characters (which includes \n), test
> for newline before spacing, and use !isspace(c) instead of isalnum(c),
> and then you can legitimately use the name given.
>
> >f_eat_spaces() will return
> >either 1 if
> >it hits an alphanum char or EOF if it hits EOF:
> >
> >int f_eat_spaces(FILE * f)
> >{
> > int rtn=2;
> >     char c;
> >
> >     /* loop until either an EOF is encountered
> >      * or an alphanumeric character or \n is found.
> >      *
> >      * Return:
> >      * 1 - if alphanumeric char is encountered
> >      * 0 - if \n is encountered
> >      * EOF - if EOF is encountered
> >      */
> >     while(rtn!=EOF)
> >     {
> >      rtn=fscanf(f,"%c",&c);
>
> What happens if you get an EOF?
> You just go ahead and process whatever is in c anyway!
> And if it happens to look like a \n or an alnum, you return that code.

not true.  If it's EOF, I return EOF so my calling function knows it hit an
EOF.
If it hits \n, it returns a 0, if it hits an alphanumeric, it returns 1.

rtn=fscanf(f,"%c",&c) means that rtn gets the return code for fscanf.
fscanf
returns either EOF, the number of conversions, or in the case of a failure,
the number of conversions before the conversion happened.  In my code, I do
a scanf
and get the return code.  If the return code is EOF, it will break out of
the while(rtn!=EOF)
loop and return EOF.  Otherwise, I set rtn to 0 if I find \n or 1 if I hit
an alphanumeric character
and then return rtn.

What you say below is that it will never return an EOF because I am scanning
in a char.  I did a
test program and it seemed to detect EOF just fine.  How it works with the
conversion, I don't
know.  But at the very least, it would come across the inconsistent
conversion and return EOF
anyway.  At least that's what I get out of the UNIX man pages on fscanf.

However, your approach of using fgetc would probably make a bit more sense.

> This could be called and return the same result indefinitely!

not true, see above.

> See below for C approach: and note the name change to reflect reality.
>
> >          /* if we come across an alphanumeric
> >           * character, we put it back and break
> >           * out of the loop.*/
> >          if(isalnum(c))
> >          {
> >           rtn=1;
> >           ungetc(c,f);
> >               break;
> >          }
> >          else if(c=='\n')
> >          {
> >            rtn=0;
> >               break;
> >          }
> >
> >     }
> >     return rtn;
> >}
>
> /* read until an alphanumeric character, \n or EOF is found.
>  * Return:
>  * 0 - if \n is encountered
>  * 1 - if alphanumeric char is encountered
>  * EOF - if EOF is encountered
>  */
> int f_eat_non_alnum(FILE * f)
> {
>     int c; /* EOF does not fit in a char, you need an int */
>
> /* normal idiomatic technique for C I/O avoids problems:
>  * get data, save the result, and test for the end condition */
>     while ((c = getc(f)) != EOF)
>     {
>         /* if newline, just return flag value */
>         if (c == '\n')
>         {
>             return 0;
>         }
>      /* else redundant because of return */
>         /* if alphanumeric, put it back, and return flag value */
>         if(isalnum(c))
>         {
>             ungetc(c,f);
>             return 1;
>         }
>     }
>
>     return c;
> }
>
> And for your eat newlines loop setup and loop:
>
> >rtn=f_eat_spaces(param);
> >          /* eat up white spaces and multiple \n characters
> >           * until an alphanumeric or EOF encountered */
> >          while(rtn==0)
> >          {
> >          rtn=f_eat_spaces(param);
> >          }
> >
> >          /* if it's EOF, we're done with parameter file */
> >          if(rtn==EOF)
> >           break;
>
> just use the same kind of C I/O idiom:
>
>     /* eat up non-alnum and \n characters
>      * until an alnum or EOF encountered */
>     while ((rtn = f_eat_non_alnum( param )) == 0)
>     {
>         continue; /* does nothing, just obvious placeholder */
>     }
>
>     /* if it's EOF, we're done with parameter file */
>     if (rtn == EOF)
>         break;
>

I understand what you're getting at, but I still don't understand why my
chunk of code had anything to do with a malloc failure.  It may be one of
the deep down low level things that I just don't get.

Thanks so much for your help!  I appreciate it!

Best regards,
John


- Raw text -


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