Mail Archives: djgpp/2004/08/12/17:45:37
> 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 -