• C语言文件操作 fseek ,fread,rewind


    通常文件打开后,读写位置按先后顺序.但有时你想变动读写位置,例如重新从某处起,再读一次.
    
    
    
    int fseek(FILE *stream, long offset, int fromwhere);fseek 用于二进制方式打开的文件,移动文件读写指针位置.
    fseek(in,-1L,1);   -- 文件流in, 零点为当前指针位置,SEEK_CUR 就是 1,  -1L -- 文件指针回退1个字节int fseek( FILE *stream, long offset, int origin );
      第一个参数stream为文件指针
      第二个参数offset为偏移量,整数表示正向偏移,负数表示负向偏移
      第三个参数origin设定从文件的哪里开始偏移,可能取值为:SEEK_CUR、 SEEK_END 或 SEEK_SET
      SEEK_SET: 文件开头
      SEEK_CUR: 当前位置
      SEEK_END: 文件结尾
      其中SEEK_SET,SEEK_CUR和SEEK_END和依次为0,1和2.
      简言之:
      fseek(fp,100L,0);把fp指针移动到离文件开头100字节处;
      fseek(fp,100L,1);把fp指针移动到离文件当前位置100字节处;
        fseek(fp,100L,2);把fp指针退回到离文件结尾100字节处。
    
    
    
     
    
    
    
    rewind
    功 能: 将文件指针重新指向一个流的开头 
    
    用 法: int rewind(FILE *stream); 
    函数原型: long ftell(FILE *fp)
    
    函数功能:得到流式文件的当前读写位置,其返回值是当前读写位置偏离文件头部的字节数.
    
     
    
     
    
     
    
     
    
     
    
     
    
    fread 
    
      功 能: 从一个流中读数据 
    
      函数原型: size_tfread(void*buffer,size_tsize,size_tcount,FILE*stream);  
    
      参 数: 
    
      1.用于接收数据的地址(指针)(buffer) 
    
      2.单个元素的大小(size) :单位是字节而不是位,例如读取一个整型数就是2个字节 
    
      3.元素个数(count) 
    
      4.提供数据的文件指针(stream) 
    
      返回值:成功读取的元素个数 程序例  #include <stdio.h> 
    
      int main(void) 
    
      { 
    
      FILE *stream; 
    
      char msg[] = "this is a test"; 
    
      char buf[20]; 
    
      if ((stream = fopen("DUMMY.FIL", "w+")) 
    
      == NULL) 
    
      { 
    
      fprintf(stderr, 
    
      "Cannot open output file.
    "); 
    
      return 1; 
    
      } 
    
      /* write some data to the file */ 
    
      fwrite(msg, strlen(msg)+1, 1, stream); 
    
      /* seek to the beginning of the file */ 
    
      fseek(stream, 0, SEEK_SET); 
    
      /* read the data and display it */ 
    
      fread(buf, strlen(msg)+1, 1,stream); 
    
      printf("%s
    ", buf); 
    
      fclose(stream); 
    
      return 0; 
    
      }
    fseek函数是 用来设定文件的当前读写位置.
    
    函数原型: int fseek(FILE *fp,long offset,int origin);
    
    函数功能:把fp的文件读写位置指针移到指定的位置.
    
    fseek(fp,20,SEEK_SET); 意思是把fp文件读写位置指针从文件开始后移20个字节.
    
    
    
    ftell函数是用来获取文件的当前读写位置;
    
    函数原型: long ftell(FILE *fp)
    
    函数功能:得到流式文件的当前读写位置,其返回值是当前读写位置偏离文件头部的字节数.
    
    ban=ftell(fp); 是获取fp指定的文件的当前读写位置,并将其值传给变量ban.
    
    
    
    fseek函数与ftell函数综合应用:
    
    分析:可以用fseek函数把位置指针移到文件尾,再用ftell函数获得这时位置指针距文件头的字节数,这个字节数就是文件的长度.
    
    #include <stdio.h>
    
    main()
    
    {
    
       FILE *fp;
    
       char filename[80];
    
       long length;
    
       printf("输入文件名:");
    
       gets(filename);
    
       //以二进制读文件方式打开文件
    
       fp=fopen(filename,"rb");
    
       if(fp==NULL)
    
          printf("file not found!
    ");
    
       else
    
          {
    
             //把文件的位置指针移到文件尾
    
              fseek(fp,OL,SEEK_END);
    
             //获取文件长度;
    
              length=ftell(fp);
    
              printf("该文件的长度为%1d字节
    ",length);
    
              fclose(fp);
    
          }
    
    }
  • 相关阅读:
    js中break/continue
    js实现连接的两种放法
    jsdate对象toLocaleString()方法小结
    接口学习小节
    c# 装箱和拆箱
    c#数据类型学习
    return 作用域
    js中break/continue
    ArcGIS Runtime for Android开发教程V2.0(9)基础篇查询检索
    【转】ArcGIS 10.1 地图发布以及缓存管理
  • 原文地址:https://www.cnblogs.com/Lee-geeker/p/3354609.html
Copyright © 2020-2023  润新知