一起talk C栗子吧(第一百九十回:C语言实例--文件定位三 )


各位看官们,大家好,上一回中咱们说的是文件定位的例子,这一回咱们继续说该例子。闲话休提,言归正转。让我们一起talk C栗子吧!


看官们,我们在上一回介绍了文件定位函数中三个函数,并且举例说明了它们的用法。这一回中,我们介绍剩下的两个函数及其使用方法。

1.fgetpos函数

函数原型:int fgetpos(FILE *stream, fpos_t *pos);

  • 该函数用来获取stream文件流在文件中的位置,并且把它存放在pos所指的变量中;
  • 该函数的第一个参数是文件指针类型的参数,通过它可以操作文件流;
  • 该函数的第二个参数是一个指针变量,用来存放获取到的文件位置信息;
  • 该函数运行成功时返回0,否则返回非零值;

2.fsetpos函数

函数原型:int fsetpos(FILE *stream, fpos_t *pos);

  • 该函数用来把stream文件流在文件中的位置设置为pos变量所指的位置;
  • 该函数的第一个参数是文件指针类型的参数,通过它可以操作文件流;
  • 该函数的第二个参数是一个指针变量,它存放着文件位置信息;可以通过fgetpos函数获取它的值;
  • 该函数运行成功时返回0,否则返回非零值;

光说不练,不是我们的风格,接下来,我们通过具体的代码来说明如何使用这些函数进行文件定位。

    //show the location and content of file
    fgetpos(fp,&tell_pos);
    printf("---location---%d---",tell_pos);
    while(index++ < 21 && fgets(buf,BUFSIZ,fp) != NULL)
    {
        printf("%s",buf);
        fgetpos(fp,&tell_pos);
        if(index == 5)
            flag_pos = tell_pos;

        printf("---location---%d---",tell_pos);
    }

    // show the conten of  flag_pos
    printf("\n----------------------------------- \n");
    fsetpos(fp,&flag_pos);
    fgets(buf,BUFSIZ,fp);
    printf("---location---%d---",flag_pos);
    printf("%s",buf);

我们在代码中打开了当前系统中的stdio.h文件,并且打印出了文件中前21行的内容和文件流的位置。同时也打印了第6行的内容和文件流位置。以上是核心代码,完整的代码放到了我的资源中,大家可以点击这里下载使用。

在编译该程序时会有警告,主要是把fpos_t类型的变量当前int类型的变量输出。因为在不同的Linux系统中,fpost_t的类型可能不一样,所以我们不作详细的说明,大家可以暂时把它当作int类型来使用,遇到编译器的警告,先忽略。下面是程序的运行结果,请大家参考:

---location---0---/* Define ISO C stdio on top of C++ iostreams.
---location---47---   Copyright (C) 1991-2014 Free Software Foundation, Inc.
---location---105---   This file is part of the GNU C Library.
---location---148---
---location---149---   The GNU C Library is free software; you can redistribute it and/or
---location---219---   modify it under the terms of the GNU Lesser General Public
---location---281---   License as published by the Free Software Foundation; either
---location---345---   version 2.1 of the License, or (at your option) any later version.
---location---415---
---location---416---   The GNU C Library is distributed in the hope that it will be useful,
---location---488---   but WITHOUT ANY WARRANTY; without even the implied warranty of
---location---554---   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
---location---623---   Lesser General Public License for more details.
---location---674---
---location---675---   You should have received a copy of the GNU Lesser General Public
---location---743---   License along with the GNU C Library; if not, see
---location---796---   <http://www.gnu.org/licenses/>.  */
---location---835---
---location---836---/*
---location---839--- *  ISO C99 Standard: 7.19 Input/output <stdio.h>
---location---888--- */
---location---892---
----------------------------------- 
---location---219---   modify it under the terms of the GNU Lesser General Public

各位看官,关于文件定位的例子咱们就说到这里。欲知后面还有什么例子,且听下回分解 。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talk_8

真诚赞赏,手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值