Experiment 12_ 6_ Familiar binary file

Title Description

The commonly used data types are character type (char type), short type (short type), integer type (int type), long type (long type), extended long type (long type), single precision floating point type (float type) and double precision floating point type (double type). The problem revolves around data types and binary files. There is a binary file named dict.dic. The C language code for generating this file is roughly as follows:
The variables are as follows:
char a,aa[5];// Note that the array AA here is a normally used string, so it will contain the character '\ 0', which does not need to be output
short b,bb[5];
int c,cc[5];
long d,dd[5];
long long e,ee[5];
float g,gg[5];
double h,hh[5];
int i ;

The code segment written is as follows:
fwrite(&a,sizeof(a),1,fp);
fwrite(&b,sizeof(b),1,fp);
fwrite(&c,sizeof©,1,fp);
fwrite(&d,sizeof(d),1,fp);
fwrite(&e,sizeof(e),1,fp);
fwrite(&g,sizeof(g),1,fp);
fwrite(&h,sizeof(h),1,fp);
fwrite(aa,sizeof(a),5,fp);
fwrite(bb,sizeof(b),5,fp);
fwrite(cc,sizeof©,5,fp);
fwrite(dd,sizeof(d),5,fp);
fwrite(ee,sizeof(e),5,fp);
fwrite(gg,sizeof(g),5,fp);
fwrite(hh,sizeof(h),5,fp);

This code segment was executed five times in a row. Of course, the data written each time is different.

Your task is to read out seven individual variables from dict.dic according to the input order and store them in the corresponding types of variables, assuming a, b, c, d, e, g and h. Then read out 7 arrays with length of 5 from the file and store them in aa[5], bb[5], cc[5], dd[5], ee[5], gg[5] and hh[5] respectively. Finally, all their values are output to the screen.
Note that all variables do not exceed their storage range.

Content tips: in the operation of the file in this topic, the C language file operation function will be used. The way to open the file is as follows:
1.FILE *fp=fopen(“file.dat”,“rb”);// "RB" is to open binary files as read-only.
2. Sample code of FREAD:
#include<stdio.h>
int main()
{
int c,cc[5]={0};
FILE *fp=fopen("file.dat","rb");
fread(&c,sizeof(int),1,fp); // Read an integer variable from the file pointed to by FP
fread(cc,sizeof(int),5,fp); // Read an integer array with length of 5 from the file pointed to by FP.
fclose(fp);
return 0;
}

input

Is an integer, which can only be one of 1, 2, 3, 4 and 5.

output

Output the relevant contents in dict.dic to the screen. If the input is 1, the content written for the first time by that code will be output. If the input is 2, the content written for the second time by that code will be output, and so on.
The form is shown in the example.

sample input

1

sample output

X
1234
2000000000
2000000000
500000000000000
1.234500
9.876543
XXX
1234 1234 1234 1234 1234
2000000000 2000000000 2000000000 2000000000 2000000000
2000000000 2000000000 2000000000 2000000000 2000000000
500000000000000 500000000000000 500000000000000 500000000000000 500000000000000
1.234500 1.234500 1.234500 1.234500 1.234500
9.876543 9.876543 9.876543 9.876543 9.876543

source code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    FILE * fp = fopen("dict.dic","rb");
    char a,aa[5];//Note that the array aa here is a normally used string, so it will contain the character '\ 0', which does not need to be output
    short b,bb[5];
    int c,cc[5];
    long d,dd[5];
    long long e,ee[5];
    float g,gg[5];
    double h,hh[5];
    int i ;
    int n;
    scanf("%d",&n);
    memset(aa,0,5);
    memset(bb,0,5);
    memset(cc,0,5);
    memset(dd,0,5);
    memset(ee,0,5);
    memset(gg,0,5);
    memset(hh,0,5);
    while(--n){
        fread(&a,sizeof(a),1,fp);
        fread(&b,sizeof(b),1,fp);
        fread(&c,sizeof(c),1,fp);
        fread(&d,sizeof(d),1,fp);
        fread(&e,sizeof(e),1,fp);
        fread(&g,sizeof(g),1,fp);
        fread(&h,sizeof(h),1,fp);
        fread(aa,sizeof(a),5,fp);
        fread(bb,sizeof(b),5,fp);
        fread(cc,sizeof(c),5,fp);
        fread(dd,sizeof(d),5,fp);
        fread(ee,sizeof(e),5,fp);
        fread(gg,sizeof(g),5,fp);
        fread(hh,sizeof(h),5,fp);
        memset(aa,0,5);
    }
    fread(&a,sizeof(a),1,fp);
    fread(&b,sizeof(b),1,fp);
    fread(&c,sizeof(c),1,fp);
    fread(&d,sizeof(d),1,fp);
    fread(&e,sizeof(e),1,fp);
    fread(&g,sizeof(g),1,fp);
    fread(&h,sizeof(h),1,fp);
    fread(aa,sizeof(a),5,fp);
    fread(bb,sizeof(b),5,fp);
    fread(cc,sizeof(c),5,fp);
    fread(dd,sizeof(d),5,fp);
    fread(ee,sizeof(e),5,fp);
    fread(gg,sizeof(g),5,fp);
    fread(hh,sizeof(h),5,fp);
    printf("%c\n",a);
    printf("%hd\n",b);
    printf("%d\n",c);
    printf("%ld\n",d);
    printf("%lld\n",e);
    printf("%f\n",g);
    printf("%lf\n",h);
    printf("%s\n",aa);
    for(i = 0;i < 4;i++){
        printf("%hd ",bb[i]);
    }
    printf("%hd\n",bb[i]);
    for(i = 0;i < 4;i++){
        printf("%d ",cc[i]);
    }
    printf("%d\n",cc[i]);
    for(i = 0;i < 4;i++){
        printf("%ld ",dd[i]);
    }
    printf("%ld\n",dd[i]);
    for(i = 0;i < 4;i++){
        printf("%lld ",ee[i]);
    }
    printf("%lld\n",ee[i]);
    for(i = 0;i < 4;i++){
        printf("%f ",gg[i]);
    }
    printf("%f\n",gg[i]);
    for(i = 0;i < 4;i++){
        printf("%lf ",hh[i]);
    }
    printf("%lf\n",hh[i]);
    fclose(fp);
    return 0;
}

I don't know what the problem is

Keywords: C

Added by slightlyeskew on Tue, 08 Mar 2022 00:09:55 +0200