曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 5652|回复: 0
打印 上一主题 下一主题

[文件I/O] fgetpos() -- 取得当前文件的句柄

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2009-7-8 05:16:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
句柄就是文件的指针。
函数原型
#include <stdio.h>
int fgetpos(FILE *stream, fpos_t *pos);
函数执行成功则返回 0 ,失败则返回 -1 。

测试代码
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

int main()
{
        fpos_t position;
        FILE *stream;

        char mingyan[1000];
        char *p = mingyan;
        stream = fopen("my4.txt", "r");
        if (stream == NULL){
                perror("errno");
                exit(1);
        }

        fread(p, 3, 1, stream);
        fgetpos(stream, &position);
        printf("pos is %d\n", position);
        printf("pos is %d\n", position.__pos);
       
         fclose(stream);
        return 0;
}
运行及输出
beyes@linux-beyes:~/BBS/3ins/ins> ./read.exe
pos is 3
pos is 3
程序中,注意一点,其实  fpos_t 是个结构体类型。它有一个成员是 __pos ,指的就是这个指针的位置。
直接用结构体名来输出,输出的是结构体中第一个成员的值。

这个 fpos_t 类型在 stdio.h 中是这么定义的:
typedef _G_fpos_t fpos_t;
而 _G_fpos 在 <_G_config.h> 中定义如下
typedef struct
{
  __off_t __pos;
  __mbstate_t __state;
} _G_fpos_t;
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-4 01:07 , Processed in 0.078416 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表