Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you have one item of variable length data (a dynamically allocated string pointer) you have to decide how you want to manage it in a binary file. You can set aside a fixed length of space for the name within the structure in your output. Or you can write a zero-terminated string and manage it that way. I'll choose the second case since it is more in line with the dynamic string.</p> <p>This is a bit verbose since the nature of the structure hasn't been given in the problem statement. So the following will work with structure elements in arbitrary order.</p> <pre><code>void WriteBinaryFile(ListDataType *Data, int numEl) { FILE *bin_file; char name[50]; printf("\n\nWhat would you like to call the binary file in which the data will be stored?\n" printf("NB remember to add \".txt\" after your chosen name!!!\nENTER NAME NOW&gt;&gt;"); scanf("%s", name); bin_file = fopen(name, "wb+"); do { //NOTE: include terminating zero in the string output; assumes single-byte chars fwrite(Data-&gt;component.name, strlen(Data-&gt;component.name)+1, 1, bin_file); fwrite(&amp;Data-&gt;component.int1, sizeof(Data-&gt;component.int1), 1, bin_file); fwrite(&amp;Data-&gt;component.int2, sizeof(Data-&gt;component.int2), 1, bin_file); fwrite(&amp;Data-&gt;component.double1, sizeof(Data-&gt;component.double1), 1, bin_file); fwrite(&amp;Data-&gt;component.double2, sizeof(Data-&gt;component.double2), 1, bin_file); fwrite(&amp;Data-&gt;component.double3, sizeof(Data-&gt;component.double3), 1, bin_file); fwrite(&amp;Data-&gt;component.double4, sizeof(Data-&gt;component.double4), 1, bin_file); Data = Data-&gt;nextPtr; } while(Data != NULL); fclose(bin_file); } </code></pre> <p>If a program needs to read it back, it would start by reading in a name and read characters until it finds a <code>0</code>. Then it would read the other elements per their data types. That process would be repeated for each structure written. This isn't necessarily portable, though. You have to be careful about size of int times, etc.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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