C文件操作的定位

/*
名称:C文件操作的定位
说明:rewind(fp):将文件读/写指针移到文件的开始位置
fseek(fp,offset,whence):将文件读/写指针从whence标识的位置移动offset个字节
其中offset可以去负值代表向后移动,在此函数中,whence取0代表文件开头,2代表
当前位置,3代表文件末尾。此函数多用于二进制文件中,文本文件可能字符转换的
过程中出现问题。

*/

#include<stdio.h>

int main()
{
    char c;
    FILE *fp;
    long pos;   //指明文件指针当前的位置

    fp = fopen("text.dat","wa+");
    if(fp == NULL)
    {
        printf("open the file wrongly!");
    }
    else
    {
        //fprintf(fp,"%s--%d\n","test",20);
        fputs("123456789abcdefghij",fp);

    }

    pos = ftell(fp);   //获得文件的当前指针
    printf("the file pointer's pos is :%ld\n",pos);

    rewind(fp);     //使指针移动到开始位置

    printf("the first character is %c.\n",fgetc(fp));

    fseek(fp,5L,0);
    printf("the fifth character is %c.\n",fgetc(fp));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值