Mail Archives: djgpp/2004/08/12/14:00:35
On Thu, 12 Aug 2004 09:25:49 -0600 in comp.os.msdos.djgpp, "John
Hanley" <jdhanley AT telusplanet DOT net> wrote:
You do realize that this routine will eat not just spacing characters,
but any and all special characters, including [+-.]?
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.
This could be called and return the same result indefinitely!
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;
If you put in a few more spaces, and keep the indentation consistent,
it makes the code easier to read, and makes it easier to see where you
may have gone wrong.
--
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada
Brian DOT Inglis AT CSi DOT com (Brian dot Inglis at SystematicSw dot ab dot ca)
fake address use address above to reply
- Raw text -