C language to write the structure body to file __c language

Source: Internet
Author: User
Tags fread
You can use Fwrite () to write a struct body to a file:
Fwrite (&some_struct,sizeof SOMESTRUCT,1,FP);
The corresponding fread function can be read again, where the fwrite is given a pointer to a structure and the memory image of the structure is written to the file as a byte stream. The sizeof operator calculates the number of bytes occupied by the structure.
However, data files written with memory images are not portable, especially if the structure contains floating-point members or pointers. The memory layout of the structure is related to both the machine and the compiler. Different compilers may use a different number of padding bits, and the size and byte order of the base types on different machines vary. Therefore, the structure written as a memory image may not be read back on another machine (even after being compiled by another compiler).
Also note that if the struct contains any pointers (char* strings or pointers to other data structures), only the pointer values will be written to the file. They may have been invalidated when they were read back again. Finally, for extensive portability, you must open the file with a "B" sign.

The procedures for reading and writing structures are as follows:

To write a struct body to a file:

#include <stdio.h> #include <stdlib.h> typedef struct {char C;
    int h;
    Short N;
    Long m;
    float F;
    Double D1;
    Char *s;
Double D2;
}st;
    int main (void) {FILE *fp;
    St SA,SB;
    Char *str= "ABCDEFG";
    sa.c= ' K ';
    sa.h=-3;
    sa.n=20;
    sa.m=100000000;
    sa.f=33.32f;
    sa.d1=78.572;
    sa.d2=33.637;
    SA.S=STR;
    Fp=fopen ("St.txt", "w+");
        if (!FP) {printf ("errror!\n");
    Exit (-1);
    printf ("sa:c=%c,h=%d,n=%d,m=%d,f=%f,d1=%f,s=%s,d2=%f\n", SA.C,SA.H,SA.N,SA.M,SA.F,SA.D1,SA.S,SA.D2); printf ("sizeof (SA) =%d:&c=%x,&h=%x,&n=%x,&m=%x,&f=%x,&d1=%x,&s=%x,&d2=%x\n",
    sizeof (SA), &AMP;SA.C,&AMP;SA.H,&AMP;SA.N,&AMP;SA.M,&AMP;SA.F,&AMP;SA.D1,&AMP;SA.S,&AMP;SA.D2);
    Fwrite (&sa,sizeof (SA), 1,FP);
    Rewind (FP);
    Fread (&sb,sizeof (SB), 1,FP);
       
    printf ("sb:c=%c,h=%d,n=%d,m=%d,f=%f,d1=%f,s=%s,d2=%f\n", SB.C,SB.H,SB.N,SB.M,SB.F,SB.D1,SB.S,SB.D2); Fclose (FP);
return 0; }

To read a struct from a file:

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

typedef struct {
    char c;
    int h;
    short N;
    Long m;
    float F;
    Double D1;
    char *s;
    Double D2;
} St;
int main (void)
{
    FILE *fp;
    St SB;
    Fp=fopen ("St.txt", "R");
    if (!FP)
    {
        printf ("errror!\n");
        Exit ( -1);
    }
    Fread (&sb,sizeof (SB), 1,FP);
    printf ("sb:c=%c,h=%d,n=%d,m=%d,f=%f,d1=%f,s=%s,d2=%f\n", sb.c,sb.h,sb.n,sb.m,sb.f,sb.d1,sb.s,sb.d2);
    printf ("sizeof (SB) =%d:&c=%x,&h=%x,&n=%x,&m=%x,&f=%x,&d1=%x,&s=%x,&d2=%x\n", sizeof (SB), &SB.C,&SB.H,&SB.N,&SB.M,&SB.F,&SB.D1,&SB.S,&SB.D2);
       
    Fclose (FP);
    
    return 0;
}

The result of compiling the GCC compiler on the Linux platform is as follows:

First, the structure is written to the file:
sa:c=k,h=-3,n=20,m=100000000,f=33.320000,d1=78.572000,s=abcdefg,d2=33.637000
sizeof (SA) =40:&c=bfb98a10,&h=bfb98a14,&n=bfb98a18,&m=bfb98a1c,&f=bfb98a20,&d1=bfb98a24 , &s=bfb98a2c,&d2=bfb98a30
sb:c=k,h=-3,n=20,m=100000000,f=33.320000,d1=78.572000,s=abcdefg,d2=33.637000

To read a struct from a file:
sb:c=k,h=-3,n=20,m=100000000,f=33.320000,d1=78.572000,s= O, d2=33.637000
sizeof (SB) =40:&c=bfbc9964,&h=bfbc9968,&n=bfbc996c,&m=bfbc9970,&f=bfbc9974,&d1= bfbc9978,&s=bfbc9980,&d2=bfbc9984

The results of compiling with the Visual C + + compiler under the Windows XP platform are as follows:

To write to the struct body:
sa:c=k,h=-3,n=20,m=100000000,f=33.320000,d1=78.572000,s=abcdefg,d2=33.637000
sizeof (SA) =48:&c=12ff28,&h=12ff2c,&n=12ff30,&m=12ff34,&f=12ff38,&d1=12ff40,&s= 12ff48,&d2=12ff50
sb:c=k,h=-3,n=20,m=100000000,f=33.320000,d1=78.572000,s=abcdefg,d2=33.637000

read out the structural body:

sb:c=k,h=-3,n=20,m=100000000,f=33.320000,d1=78.572000,s=e,d2=33.637000
sizeof (SB) =48:&c=12ff28,&h=12ff2c,&n=12ff30,&m=12ff34,&f=12ff38,&d1=12ff40,&s= of

From the above results we can get the following conclusions:

1. If the structure contains pointers, is very easy to go wrong, from the above results (highlighted) part of the result of the string output is not the same, which means that when the file is written, the string char* to the file is not written, just write the pointer, when the structure is read from the file, When you get this pointer again, because the program is running memory location changes, so the original pointer to the content also changed, so the output has changed.

2. There is also a more important problem with the memory of the structure (discussed before). As you can see, different compilers take the same approach. The structure body size in GCC is 40 bytes, and VC is 48 bytes.

And GCC, the structure is stored in the starting position is a multiple of 4, and VC is a multiple of 8. This is because the starting address of the struct body is related to the type in which it contains the most bytes. As has been said before, because GCC is handled in a structure with more than 4 byte types, the starting position of the structure begins with a multiple of 4, and the VC is the maximum number of bytes.

Ps:4 is a multiple of the address of the minimum two digits is the 00,8 of the address of the minimum three digits to 000 then I read the WinXP in Linux under the structure of the file and read under the Linux under the structure of the WinXP, two platform under the program are collapsed.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.