Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You read a record to uninitialized pointer, I guess you should alloc memory first.</p> <pre><code>void *record = maloc( size ) </code></pre> <p>And do not forget to free ...</p> <p>May I suggest you to use a Union?</p> <p>Your type definitions will look like this:</p> <pre><code>struct SType{ int type;//struct type }; struct S1{ }; struct S2{ }; struct S3{ }; union S{ S1 s1; S2 s2; S3 s3; }; </code></pre> <p>now read and write can be done like this: </p> <pre><code>SType stype; S s; fread(&amp;stype,sizeof(stype),1,file); size = ??? //get according to type fread(&amp;s,size,1,file); // your data will be stored according to type in s.s1, s.s2 or s.s3 size = ??? //get according to type fwrite(&amp;stype,sizeof(stype),1,file); fwrite(&amp;s,size,1,file); </code></pre> <p>Next stage, is to unify the Type with the rest:</p> <pre><code>struct S{ int type;//struct type union { S1 s1; S2 s2; S3 s3; }s; }; /* in case you don't care about loosing file space, you can read\write all in a single operation like this: */ S s; fread(&amp;s,sizeof(s),1,file); // now according to the type you take s.s.s1, s.s.s2 or s.s.s3. fwrite(&amp;s,sizeof(s),1,file); /* if you do care about loosing file space, you work like before */ S s; fread(&amp;s.type,sizeof(int),1,file); size = ??? //get according to type fread(&amp;s.s,size,1,file); // as before, you take s.s.s1, s.s.s2 or s.s.s3. size = ??? //get according to type fwrite(&amp;s.type,sizeof(int),1,file); fwrite(&amp;s.s,size,1,file); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload