EOF= -1 ???

I have a simple code to get interger from a file.. but have problem
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
FILE* f;
f=fopen("test.txt","r");
int number;
do
{
fscanf(f,"%d",&number);
}
while(number!=EOF);


}


But, the output is wrong.. because EOF = -1;
ex: test.txt
1 2 3 4 5 6 -1 7 8 9

the while loop stop at -1.. doesn't get 7 8 9 from file..
can you give me a solution for this.. thank you
Last edited on
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    FILE* f;
    f=fopen("test.txt","r");
    int number;
    while( ! feof(f) )   //; oops, ';' should not be here
    {
        fscanf(f,"%d",&number);
    }
    fclose(f);
}

NB:untested code

Edit: coding error
Last edited on
thanks grey wolf..
oh and in your code you were reading formated text, the same as you would read a txt file with your eyes but char(-1) is a special character (EOF) and it doesn't even show... you need to read a character and than test it(i don't use fscanf but to read a character you musn't use %d)
Topic archived. No new replies allowed.