zoukankan      html  css  js  c++  java
  • 函数fseek() 用法(转)

    在阅读代码时,遇到了很早之前用过的fseek(),很久没有用了,有点陌生,写出来以便下次查阅。

    函数功能是把文件指针指向文件的开头,需要包含头文件stdio.h

    fseek   函数名: fseek   功 能: 重定位流上的文件指针   用 法: int fseek(FILE *stream, long offset, int fromwhere);   描 述: 函数设置文件指针stream的位置。如果执行成功,stream将指向以fromwhere为基准,偏移offset个字     节的位置。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。   返回值: 成功,返回0,否则返回其他值。   fseek position the file position pointer for the file referenced by stream to the byte location calculated by offset.   程序例:   

    1. #include <stdio.h>  
    2.   long filesize(FILE *stream);  
    3.   int main(void)  
    4.   {  
    5.     FILE *stream;  
    6.     stream = fopen("MYFILE.TXT", "w+");  
    7.     fprintf(stream, "This is a test");  
    8.     printf("Filesize of MYFILE.TXT is %ld bytes ", filesize(stream));  
    9.     fclose(stream);  
    10.     return 0;  
    11.   }  
    12.   long filesize(FILE *stream)  
    13.   {  
    14.     long curpos, length;  
    15.     curpos = ftell(stream);  
    16.     fseek(stream, 0L, SEEK_END);  
    17.     length = ftell(stream);  
    18.     fseek(stream, curpos, SEEK_SET);  
    19.     return length;  
    20.   }  

      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字节处。   使用实例:

    1. #include <stdio.h>  
    2. #define N 5  
    3. typedef struct student {  
    4.  long sno;  
    5.  char name[10];  
    6.  float score[3];  
    7. } STU;  
    8. void fun(char *filename, STU n)  
    9. {  
    10.  FILE *fp;  
    11.  fp = fopen(filename, "rb+");  
    12.  fseek(fp, -1L*sizeof(STU),SEEK_END);  
    13. fwrite(&n, sizeof(STU), 1, fp);  
    14. fclose(fp);  
    15. }  
    16. void main()  
    17. {  
    18.   STU t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},  
    19.   {10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},  
    20.   {10005,"ZhangSan", 95, 80, 88}};  
    21.   STU n={10006,"ZhaoSi", 55, 70, 68}, ss[N];  
    22.   int i,j; FILE *fp;  
    23.   fp = fopen("student.dat", "wb");  
    24.   fwrite(t, sizeof(STU), N, fp);  
    25.   fclose(fp);  
    26.   fp = fopen("student.dat", "rb");  
    27.   fread(ss, sizeof(STU), N, fp);  
    28.   fclose(fp);  
    29.   printf(" The original data : ");  
    30.   for (j=0; j<N; j++)  
    31.   {  
    32.    printf(" No: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);  
    33.    for (i=0; i<3; i++)   
    1.  printf("%6.2f ", ss[j].score[i]);  
    2.  printf(" ");  
    3. }  
    4. fun("student.dat", n);  
    5. printf(" The data after modifing : ");  
    6. fp = fopen("student.dat", "rb");  
    7. fread(ss, sizeof(STU), N, fp);  
    8. fclose(fp);  
    9. for (j=0; j<N; j++)  
    10. {  
    11.  printf(" No: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);  
    12.  for (i=0; i<3; i++)   
    1.  printf("%6.2f ", ss[j].score[i]);  
    2.  printf(" ");  
    3. }  

    转自:http://blog.csdn.net/wl_soft50/article/details/7787521

  • 相关阅读:
    shell脚本修改文件
    腾讯企业邮箱获取客户端专用密码(授权码)
    java内存dump文件导出与查看
    为什么MySQL数据库索引选择使用B+树?
    nginx 平滑重启的实现方法
    nginx重启 平滑重启
    Nginx常用的平滑重启
    nginx reload和reopen
    转载【小程序】: 微信小程序开发---应用与页面的生命周期
    【微信小程序】用户首次进入小程序拒绝授权,如何再次调用授权页面,获取用户信息userInfo
  • 原文地址:https://www.cnblogs.com/YangBinChina/p/4205399.html
Copyright © 2011-2022 走看看