3

I am learning C using this site.

Under fgetc() Function the author said:

This function reads a single character from a file and after reading increments the file position pointer.

FILE *fp;
fp = fopen(filename, "r");

What I want to ask is that the file position pointer is different from the pointer fp or not?

alk
  • 69,737
  • 10
  • 105
  • 255
Cody
  • 2,480
  • 5
  • 31
  • 62
  • There is a difference between pointing to a file and pointing to a position in a file. – John Coleman Sep 25 '16 at 14:16
  • @JohnColeman so both are different – Cody Sep 25 '16 at 14:19
  • 1
    Yes. You need to keep `fp` pointing to the file itself while the position pointer advances through the file. Otherwise -- how are you going to close it when you are done? – John Coleman Sep 25 '16 at 14:21
  • Compare a pointer to a string and having an *index* pointing to a character inside that string. – Jongware Sep 25 '16 at 14:23
  • 1
    The pointer "`FILE* fp`" is a pointer in the sense of the C language's pointer variables. The "*file position pointer*" is a pointer in the sense of the indexing-pattern: It refers/points/marks/identifies to a specific index/position in a file. – alk Sep 25 '16 at 14:32

2 Answers2

5

It means your current offset in the file. It is the return value of ftell.

v7d8dpo4
  • 1,399
  • 8
  • 9
4

No, they are not the same. fp is a pointer to a structure FILE. The file position pointer points to the position in the file, obviously.

You can find this by looking at stdio.h in your include path. In FreeBSD, FILE is defined as:

struct __sFILE {
    unsigned char *_p;  /* (*) current position in (some) buffer */
    int _r;     /* (*) read space left for getc() */
    int _w;     /* (*) write space left for putc() */
    short   _flags;     /* (*) flags, below; this FILE is free if 0 */
    short   _file;      /* (*) fileno, if Unix descriptor, else -1 */
    struct  __sbuf _bf; /* (*) the buffer (at least 1 byte, if !NULL) */
    int _lbfsize;   /* (*) 0 or -_bf._size, for inline putc */
/* operations */
void    *_cookie;   /* (*) cookie passed to io functions */
int (*_close)(void *);
int (*_read)(void *, char *, int);
fpos_t  (*_seek)(void *, fpos_t, int);
int (*_write)(void *, const char *, int);

/* separate buffer for long sequences of ungetc() */
struct  __sbuf _ub; /* ungetc buffer */
unsigned char   *_up;   /* saved _p when _p is doing ungetc data */
int _ur;        /* saved _r when _r is counting ungetc data */

/* tricks to meet minimum requirements even when malloc() fails */
unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
unsigned char _nbuf[1]; /* guarantee a getc() buffer */

/* separate buffer for fgetln() when line crosses buffer boundary */
struct  __sbuf _lb; /* buffer for fgetln() */

/* Unix stdio files get aligned to block boundaries on fseek() */
int _blksize;   /* stat.st_blksize (may be != _bf._size) */
fpos_t  _offset;    /* current lseek offset */

struct pthread_mutex *_fl_mutex;    /* used for MT-safety */
struct pthread *_fl_owner;  /* current owner */
int _fl_count;  /* recursive lock count */
int _orientation;   /* orientation for fwide() */
__mbstate_t _mbstate;   /* multibyte conversion state */
int _flags2;    /* additional flags */
};
typedef struct __sFILE FILE;
Rob
  • 14,746
  • 28
  • 47
  • 65