您的位置:首页 > 编程语言 > C语言/C++

C语言及程序设计进阶例程-37 二进制文件及其读写

2015-06-29 21:12 507 查看
贺老师教学链接 C语言及程序设计进阶 本课讲解

对比ASCII文件和二进制文件

//(1)将short int x=12321写入文本文件
#include<stdio.h>
#include<stdlib.h>

int main( )
{
    short int x=12321;
    FILE *outfile = fopen("ascii.txt", "w");
    if(outfile==NULL)
    {
        printf("Cannot open file!");
        exit(1);
    }
    fprintf(outfile, "%d", x);
    fclose(outfile);
    return 0;
}


//(2)将short int x=12321写入二进制文件
#include<stdio.h>
#include<stdlib.h>

int main( )
{
    short int x=12321;
    FILE *outfile = fopen("binary.dat", "wb");
    if(outfile==NULL)
    {
        printf("Cannot open file!");
        exit(1);
    }
    fwrite((char*)&x, sizeof(short int), 1, outfile);
    fclose(outfile);
    return 0;
}


读写二进制文件

#include<stdio.h>
#include<stdlib.h>
typedef struct
{
    int num;
    char name[10];
    int age;
    char addr[15];
} Student;

int main()
{
    FILE *fp;
    Student stu1[10], stu2[10];
    int i;
    if((fp=fopen("stu_list.dat","wb+"))==NULL)  /*用读写二进制文件的方式打开*/
    {
        printf("Cannot open file!");
        exit(1);
    }
    printf("input data\n");
    for(i=0; i<10; i++)
        scanf("%d%s%d%s",&stu1[i].num,stu1[i].name,&stu1[i].age,stu1[i].addr);
    fwrite((char*)stu1, sizeof(Student), 10, fp);   /*一次性地将多名学生数据写入文件*/
    rewind(fp);  /*将文件位置指针移到文件的开始位置*/

    printf("number\tname\tage\taddr\n");
    for(i=0; i<10; i++)
    {
        fread((char*)&stu2[i], sizeof(Student), 1, fp); /*每次循环读出1名学生数据*/
        printf("%d\t%s\t%d\t%s\n",stu2[i].num, stu2[i].name, stu2[i].age, stu2[i].addr);
    }
    fclose(fp);
    return 0;
}


例:二进制文件的随机读写

//修改stu_list.dat文件中第6位同学的年龄
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
    int num;
    char name[10];
    int age;
    char addr[15];
} Student;
int main()
{
    FILE *fp;
    Student stu;
    int i;
    if((fp=fopen("stu_list.dat","rb+"))==NULL)  /*用读写二进制文件的方式打开*/
    {
        printf("Cannot open file!");
        exit(1);
    }
    fseek(fp, 5*sizeof(Student), SEEK_SET); /*将文件读写位置指针指向第6位同学*/
    fread((char*)&stu, sizeof(Student), 1, fp);/*读出这名同学的数据*/
    stu.age = 20; /*修改年龄*/
    fseek(fp, -1*sizeof(Student), SEEK_CUR);/*出后移动了位置指针,现在从当前位置向文件起始位置的方向移动sizeof(Student)的偏移量,重新指向第6位同学*/
    fwrite((char*)&stu, sizeof(Student), 1, fp);/*写入修改后的记录*/
    fclose(fp);
    return 0;
}


学生数据处理

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct student
{
    int num;
    char name[20];
    float score;
} Student;

int main( )
{
    FILE *iofile;
    int i;
    if((iofile=fopen("stu_list.dat","wb+"))==NULL)  /*用读写方式打开*/
    {
        printf("Cannot open file!");
        exit(1);
    }
    Student stud[5]= {{1001,"Li",85},{1002,"Fun",97.5},{1004,"Wang",54},{1006,"Tan",76.5},{1010,"ling",96}};
    //(1)向磁盘文件输出5个学生的数据并显示出来
    printf("(1)向磁盘文件输出5个学生的数据并显示出来\n");
    for(i=0; i<5; i++)
    {
        fwrite((char*)&stud[i], sizeof(stud[i]), 1, iofile);
        printf("%d %s %.1f\n", stud[i].num, stud[i].name, stud[i].score);
    }

    //(2)将磁盘文件中的第1,3,5个学生数据读入程序,并显示出来;
    printf("(2)将磁盘文件中的第1,3,5个学生数据读入程序,并显示出来\n");
    Student stud1[3];                  //用来存放从磁盘文件读入的数据
    for(i=0; i<5; i=i+2)
    {
        fseek(iofile, i*sizeof(stud[i]),SEEK_SET);  //定位于第0,2,4学生数据开头
        fread((char *)&stud1[i/2], sizeof(stud1[0]), 1, iofile);
        //先后读入3个学生的数据,存放在stud1[0],stud[1]和stud[2]中
        printf("%d %s %.1f\n", stud1[i/2].num, stud1[i/2].name, stud1[i/2].score);
        //输出stud1[0],stud[1]和stud[2]各成员的值
    }

    //(3) 将第3个学生的数据修改后存回磁盘文件中的原有位置。
    printf("(3)将第3个学生的数据修改后存回磁盘文件中的原有位置\n");
    stud[2].num=1012;                         //修改第3个学生(序号为2)的数据
    strcpy(stud[2].name, "Wu");
    stud[2].score=60;
    fseek(iofile, 2*sizeof(stud[0]),SEEK_SET);   //定位于第3个学生数据的开头
    fwrite((char *)&stud[2], sizeof(stud[2]), 1, iofile); //更新第3个学生数据

    //(4)从磁盘文件读入修改后的5个学生的数据并显示出来。
    printf("(4)从磁盘文件读入修改后的5个学生的数据并显示出来\n");
    fseek(iofile, 0, SEEK_SET);                       //重新定位于文件开
    for(i=0; i<5; i++)
    {
        fread((char *)&stud[i],sizeof(stud[i]),1,iofile);  //读入5个学生的数据
        printf("%d %s %.1f\n", stud[i].num, stud[i].name, stud[i].score);
    }
    fclose(iofile);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: