Too many EOFs

Hello,

I am having a hell of a time understanding why the following code returns multiple EOF indicators:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int main()
{
	FILE *f;
	f = fopen ("data.txt","r");

	int c;
	long sz;
	fseek (f,0,SEEK_END);
	sz = ftell (f);
	//rewind (f);
	fseek (f,0,SEEK_SET);
	printf ("size of file = %d\n",sz);
	for (unsigned int i = 0; i<sz ; ++i)
	{
		c = fgetc (f);
		printf ("pos %d = %d\n",i,c);
	}
	fclose (f);
        return 0;
}


I get one EOF (denoted by my compiler as a -1) per every new line in data.txt. IE if there are 4 separate lines then I will have 4 EOFs printed out. Does anyone know why?

Thanks
Last edited on
http://www.cplusplus.com/forum/windows/10853/

That link might give you thoughts on how to get around what your trying to do...
Open the file as binary.
 
f = fopen("data.txt", "rb");
Topic archived. No new replies allowed.