zoukankan      html  css  js  c++  java
  • fseek()

    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函数获得这时位置指针距文件头的字节数,这个字节数就是文件的长度.

     
      1. #include <stdio.h>  
      2. main()  
      3. {  
      4.    FILE *fp;  
      5.    char filename[80];  
      6.    long length;  
      7.    printf("Input the file name:");  
      8.    gets(filename);  
      9.    fp=fopen(filename,"rb");  
      10.    if(fp==NULL)  
      11.        printf("file not found!/n");  
      12.    else  
      13.    {  
      14.        fseek(fp,OL,SEEK_END);  
      15.        length=ftell(fp);  
      16.        printf("the file length %1d bytes/n",length);  
      17.        fclose(fp);  
      18.    }  
      19. }  
  • 相关阅读:
    Docker 镜像
    Docker 安装命令
    Docker 基本概念
    Redis 高可用之"持久化"
    Git 安装和使用
    oracle角色
    oracle权限
    审计
    手动创建数据库
    oracle口令文件认证
  • 原文地址:https://www.cnblogs.com/herizai/p/3132785.html
Copyright © 2011-2022 走看看