Failed to read the array after saving it in C language

-sharpinclude<stdio.h>
-sharpinclude<stdlib.h>
-sharpinclude<time.h>

int main()
{
    int a[10][10];
    srand(time(NULL));
    int i,j;
    for(i=0;i<10;iPP)
    {
        for(j=0;j<10;jPP)
        {
            a[i][j]=rand()%10;
            printf("%d\t",a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    FILE *fp=fopen("./a_matrix","wb+");
    if(fp)
    {
        int size=fwrite(a,sizeof(a),1,fp);
        if(size==1)
        {
            int b[10][10];
            fread(b,sizeof(b),1,fp);
            if(feof(fp))
            {
                for(i=0;i<10;iPP)
                {
                    for(j=0;j<10;jPP)
                    {
                        printf("%d\t",b[i][j]);
                    }
                    printf("\n");
                }
            }
        }
        fclose(fp);
    }
    return 0;
}

the result of a compilation run using gcc is
8 3 5 9 4 9 0 5 27
4 2 2 3 4 9 4 6 3 7
6 7 1 1 4 4 7 0 3 5
9 40 6 3 5 7 5 22
4 8 4 6 7 8 6 3
5 3 0 6 4 6 2 4 7 6
1 8 0 2 7 5 9 4 3 3
6 9 2 3 8 6 7 7 4 0
0 25 2 8 9 9 3 5 6
9 7 4 1 9 8 0 8 3

2 32546 510347552 510347552 32766 30 510347536 32766
00-769542344 32546 00 10-769542384 32546
00 1700966438 0-769541480 32546 510347704 32766 510347760 32766
-769542384 32546 00-771763729 32546 00 510347760 32766
00 00-769542384 32546-774158457
00 510347536 32766 510347552 32766-769541480 32546 32546
0 510347568 32766-1000 510943848 32766
-769542384 32546 000 1970169159 0
9 0-771799456 32546 510347720 32766 15775231 01 0
-449672227 22095-771741280 32546 00-449672304 22095-449673168 22095

I don"t understand why there is such a result

C
Mar.12,2022

can you describe what you are going to do? My understanding is that you want to write a set of random number matrices generated in memory to a blank file, and then read from the file to verify that the write is successful? But your code doesn't have any process of writing arrays to files? Moreover, in this way, writing and reading must be carried out separately, otherwise it will lead to file pointer confusion, preferably divided into two files and two programs, followed by a program and two functions. In addition, the write operation of fwrite () is actually written to the memory buffer rather than directly to the file, so you need to use fflush () or fclose () to force the contents of the buffer to be flushed to the file, otherwise there may be no content in the file. Finally, the data read by fread () in your code is always stored in the same memory location b, so the results shown can't be correct.
attach a modified code as a reference:

-sharpinclude<stdio.h>
-sharpinclude<stdlib.h>
-sharpinclude<time.h>

void write_matrix(FILE *fp)
{
    int a[10][10];
    srand(time(NULL));
    int i, j;
    for(i=0;i<10;iPP)
    {
        for(j=0;j<10;jPP)
        {
            a[i][j]=rand()%10;
            fwrite(&a[i][j], sizeof(int), 1, fp);
            printf("%d\t", a[i][j]);
        }
        printf("\n");
    }
}

void read_matrix(FILE *fp, int(*b)[10])
{
    int i, j;
    for(i=0;i<10;iPP)
    {
        for(j=0;j<10;jPP)
        {
            fread(&b[i][j], sizeof(int), 1, fp);
        }
    }
}

int main()
{
    FILE *fp = fopen("./a_matrix", "wb");
    if(fp)
    {
        write_matrix(fp);
        fclose(fp);
    }
    printf("\n");

    fp = fopen("./a_matrix", "rb");
    if(fp)
    {
        int b[10][10];
        int i, j;
        read_matrix(fp, b);
        for(i=0;i<10;iPP)
        {
            for(j=0;j<10;jPP)
            {
                printf("%d\t", b[i][j]);
            }
            printf("\n");
        }
        fclose(fp);
    }
    return 0;
}

you use fwrite to write to the file, followed by fread , the program will read from the end of the file, of course it's wrong. Use rewind (fp) to reset the file location before fread .

Menu