【问题标题】:What is file position pointer?什么是文件位置指针?
【发布时间】:2017-02-02 21:49:22
【问题描述】:

我正在使用this 站点学习 C。

fgetc()函数下作者说:

此函数从文件中读取单个字符,并在读取后递增文件位置指针。

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

我想问的是file position pointer和指针fp不一样吗?

【问题讨论】:

  • 指向文件和指向文件中的位置是有区别的。
  • @JohnColeman 所以两者都不同
  • 是的。当位置指针在文件中前进时,您需要保持fp 指向文件本身。否则 - 完成后如何关闭它?
  • 比较一个指向字符串的指针,并有一个 index 指向该字符串中的一个字符。
  • 指针“FILE* fp”是C语言指针变量意义上的指针。 “文件位置指针”是索引模式意义上的指针:它引用/指向/标记/标识文件中的特定索引/位置。

标签: c pointers file-handling


【解决方案1】:

不,它们不一样。 fp 是指向结构 FILE 的指针。显然,文件位置指针指向文件中的位置。

您可以通过查看包含路径中的stdio.h 来找到它。在 FreeBSD 中,FILE 定义为:

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;

【讨论】:

  • 你能指出文件结构的哪个成员指示文件位置指针吗?
  • 那是 *curp 吗?
  • @Cody 我在某个网站上发现了它。我试图在我的 stdio.h 中找到它,但一直被打断。这取决于我相信的操作系统。在 FreeBSD 上,情况与此完全不同。编辑:我编辑了我的答案以显示 FreeBSD 的定义。
  • @alk 是的,已修复。
  • 这个fpos_t _offset; 成员可能代表“文件位置指针”。
【解决方案2】:

这表示您当前在文件中的偏移量。是ftell的返回值。

【讨论】:

    猜你喜欢
    • 2013-12-09
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 2012-09-15
    • 2023-03-16
    • 1970-01-01
    • 2014-01-22
    相关资源
    最近更新 更多