Why isn't this working?

I'm making an XML parser, and right now I'm trying to debug it. I've narrowed the current bug down to my read_whitespace function, which is supposed to skip any whitespace.

1
2
3
static void read_whitespace(FILE* file){
	while(is_whitespace(fgetc(file)) && !feof(file)){}
	fseek(file,-1,SEEK_CUR);}

The fseek is to make sure that calling fgetc will return a non-whitespace, but for some reason I don't think this is working. Why isn't it?
in which library, have you found is_whitespace(char)?? try isspace(char) function.
It's a function I made:

1
2
3
4
static bool is_whitespace(chrtype c){
	if(c==' ' || c=='\t' || c=='\n' || c=='\r'){
		return true;}
	return false;}

And before you ask, typedef char chrtype;
Last edited on
i tried your code its working fine for me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <ctype.h>

typedef char chrtype;

static bool is_whitespace(chrtype c)
{
        if(c==' ' || c=='\t' || c=='\n' || c=='\r')
        {
                return true;
        }
        return false;
}

static void read_whitespace(FILE* file)
{
        while(is_whitespace(fgetc(file)) && !feof(file))
        {
        }
        fseek(file,-1,SEEK_CUR);
}

int main()
{
        FILE *file = fopen("input.txt", "r");
        read_whitespace(file);
        printf("%d\n", fgetc(file));
        fclose(file);
}
I figured it out - it wasn't the function that was the problem, but the various while loops that checked for non-whitespace. By not moving back 1 after every while loop, the file pointer was off by a ton by the time the error was returned.
Topic archived. No new replies allowed.