i have a small doubt regarding the FILE structure. i looked it up in the header file (i have VC++ 2008).

In this implementation, it was declared as:

struct _iobuf {
        char *_ptr;
        int   _cnt;
        char *_base;
        int   _flag;
        int   _file;
        int   _charbuf;
        int   _bufsiz;
        char *_tmpfname;
typedef struct _iobuf FILE;

here, _ptr is a the current position of the file pointer right? But im not getting the expected output in this case:

int main(void)
	FILE *fp;
	int c = 0; 

	if ( (fp = fopen("new.txt", "r")) == NULL ) {
	} else {		
		while ( c != EOF ) {			
			c = fgetc(fp);        // advance pointer
			printf("%c", *(fp->_ptr));			

	return 0;

Contents of new.txt: hello world
Output: ello world=h

Can anyone please explain why or how this is happening?

Edited 6 Years Ago by myk45: n/a

The pointer is advanced after the fgetc, so when you look at it, it is always one character ahead of what you have just read. After you have read the last character, it is pointing to garbage.

This question has already been answered. Start a new discussion instead.