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

来源:互联网 发布:java字符串包含字符串 编辑:程序博客网 时间:2024/05/28 15:17

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


看官们,我们在上一回介绍了文件定位的概念并且引出了文件定位操作的函数,当时只是简单地概述了这些函数的功能。这一回中, 我们将详细介绍如何使用这些函数来进行文件定位操作。

1.ftell函数

函数原型:long ftell(FILE *stream);

  • 该函数用来返回与stream对应的文件流在文件中的位置;
  • 该函数只有一个文件指针类型的参数,通过它可以操作文件流;
  • 该函数运行成功时返回与参数关联的文件流在文件中的位置;否则返回-1;

2.fseek函数

函数原型:int fseek(FILE *stream, long offset, int whence);

  • 该函数的第一个参数是文件类型的指针,通过它可以操作文件流;
  • 该函数的第二个参数是文件位置移动的距离;它的值可以是正数也可以是负数;最好使用ftell的返回值;
  • 该函数的第三个参数是文件流移动前的初始位置,它的值是以下三种:
    • SEEK_SET 表示文件流移动的初始位置为文件头,也就是刚刚打开文件是文件流所在的位置;
    • SEEK_END表示文件流移动的初始位置为文件尾;
    • SEEK_CUR 表示文件流移动的初始位置为当前文件流所在的位置;
  • 该函数用来定位文件,它会把与steam对应的文件流从whence指定的初始位置跳转到距离为offset的新位置。
  • 该函数运行成功时返回零,否则返回非零值;

3.rewind函数

函数原型: void rewind(FILE *stream);

  • 该函数只有一个文件指针类型的参数,通过它可以操作文件流;
  • 该函数用来把文件流从当前位置跳转到文件头;
  • 该函数总是运行成功,并且没有返回值;

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

    //show the location and content of file    tell_pos = ftell(fp);    printf("---location---%d---",tell_pos);    while(index++ < 21 && fgets(buf,BUFSIZ,fp) != NULL)    {        printf("%s",buf);        tell_pos = ftell(fp);        if(index == 5)            flag_pos = tell_pos;        printf("---location---%d---",tell_pos);    }    // show the conten of  flag_pos    printf("\n----------------------------------- \n");    fseek(fp,flag_pos,SEEK_SET);    fgets(buf,BUFSIZ,fp);    printf("---location---%d---",flag_pos);    printf("%s",buf);    // go back to the begain of file    printf("\n----------------------------------- \n");    rewind(fp);    tell_pos = ftell(fp);    fgets(buf,BUFSIZ,fp);    printf("---location---%d---",tell_pos);    printf("%s",buf);

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

下面是程序的运行结果,请大家参考:

---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----------------------------------- ---location---0---/* Define ISO C stdio on top of C++ iostreams.

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


0 0