zoukankan      html  css  js  c++  java
  • fseek函数

    fseek函数:

    int fseek(FILE *_FILE, long _Offset, int _Origin);

    函数设置文件指针stream的位置,如果执行成功,stream将指向以fromwhere为基准,偏移量offset(指针偏移量)个字节的位置,函数返回0。如果执行失败则不改变strea.m指向的位置,函数返回一个0。

    #include<stdio.h>
    #include<string.h>
    
    struct student
    {
        char name[100];
        int age;
    };
    
    int main()
    {
        struct student st = {0};
        FILE *p = fopen("./c.txt","rb");
        
        memset(&st,0,sizeof(struct student));
        fread(&st,sizeof(struct student),1,p);
        printf("name = %s,age = %d
    ",st.name,st.age);
    
        fclose(p);
        return 0;
    }

    运行结果:

    exbot@ubuntu:~/wangqinghe/C/20190725$ ./fseek

    name = wang,age = 21

    #include<stdio.h>
    #include<string.h>
    
    struct student
    {
        char name[100];
        int age;
    };
    
    int main()
    {
        struct student st = {0};
        FILE *p = fopen("./c.txt","rb");
        
        memset(&st,0,sizeof(struct student));
        fread(&st,sizeof(struct student),1,p);
        printf("name = %s,age = %d
    ",st.name,st.age);
    
        fseek(p,0-sizeof(struct student),SEEK_CUR);
    
        memset(&st,0,sizeof(struct student));
        fread(&st,sizeof(struct student),1,p);
        printf("name = %s,age = %d
    ",st.name,st.age);
    
        fclose(p);
        return 0;
    }

    运行结果:

    exbot@ubuntu:~/wangqinghe/C/20190725$ ./fseek

    name = wang,age = 21

    name = wang,age = 21

    #include<stdio.h>
    #include<string.h>
    
    struct student
    {
        char name[100];
        int age;
    };
    
    int main()
    {
        struct student st = {0};
        FILE *p = fopen("./c.txt","rb");
        
        memset(&st,0,sizeof(struct student));
        fread(&st,sizeof(struct student),1,p);
        printf("name = %s,age = %d
    ",st.name,st.age);
    
        //fseek(p,0-sizeof(struct student),SEEK_CUR);
    
        memset(&st,0,sizeof(struct student));
        fread(&st,sizeof(struct student),1,p);
        printf("name = %s,age = %d
    ",st.name,st.age);
    
        fclose(p);
        return 0;
    }

    运行结果:

    exbot@ubuntu:~/wangqinghe/C/20190725$ ./fseek

    name = wang,age = 21

    name = qing,age = 34

    #include<stdio.h>
    #include<string.h>
    
    struct student
    {
        char name[100];
        int age;
    };
    
    int main()
    {
        struct student st = {0};
        FILE *p = fopen("./c.txt","rb");
        
        memset(&st,0,sizeof(struct student));
        fread(&st,sizeof(struct student),1,p);
        printf("name = %s,age = %d
    ",st.name,st.age);
    
        //fseek(p,0-sizeof(struct student),SEEK_CUR);
        fseek(p,0-sizeof(struct student),SEEK_END);
    
        memset(&st,0,sizeof(struct student));
        fread(&st,sizeof(struct student),1,p);
        printf("name = %s,age = %d
    ",st.name,st.age);
    
        fseek(p,0-sizeof(struct student),SEEK_END);
    
        memset(&st,0,sizeof(struct student));
        fread(&st,sizeof(struct student),1,p);
        printf("name = %s,age = %d
    ",st.name,st.age);
    
        fclose(p);
        return 0;
    }

    运行结果:

    exbot@ubuntu:~/wangqinghe/C/20190725$ ./fseek

    name = wang,age = 23

    name = he,age = 557

    name = he,age = 557

  • 相关阅读:
    图的概述
    "《算法导论》之‘排序’":线性时间排序
    “《算法导论》之‘查找’”:散列表
    如何使用VS2013本地C++单元测试框架
    “《算法导论》之‘查找’”:顺序查找和二分查找
    查找算法概述
    第二部分 位运算符、赋值运算符、三元及一元运算符和语句分类
    LINQ 的查询_联表、分组、排序
    第二部分 关系与比较运算符 、 自增与自减运算符、条件逻辑运算符
    LINQ to Sql系列一 增,删,改
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11260662.html
Copyright © 2011-2022 走看看