Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulating a 3D array of structures in C
    primarykey
    data
    text
    <p>I have a 3D array of structures. The 3D grid is such that each of the three sides are equal. Each cell of the 3D grid has 5 elements, say color, temperature, B(x), B(y) and B(z). I have to populate each cell of the array of structures with 5 binary files, one for each element. </p> <p>The array of structures look like this:</p> <pre><code>struct physical { float color; float temperature; float Bx,By,Bz; }; extern struct physical ***physical; </code></pre> <p>What I need is some idea how I would carry out the populating of the arrays in C.</p> <p>I have the following code that I have implemented to read the data from the binary files into the array of structures:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;math.h&gt; int i,j,k,ibox; /* Loop indices for the physical grid */ FILE *p,*q,*r,*s,*t; p = fopen("phys_col_0107.bin","rb"); q = fopen("phys_temp_0107.bin","rb"); r = fopen("phys_Bx_0107.bin","rb"); s = fopen("phys_By_0107.bin","rb"); t = fopen("phys_Bz_0107.bin","rb"); if (!p) { printf("Unable to open color file!"); return 0; } else if (!q) { printf("Unable to open the temp file!"); return 0; } else if (!r) { printf("Unable to open the Bx file!"); return 0; } else if (!s) { printf("Unable to open the By file!"); return 0; } else if (!t) { printf("Unavle to open the Bz file!"); return 0; } for ( j = 0 ; j &lt; ny ; j++ ) { for (k=0;k&lt;nz;k++) { for (i=0;i&lt;nx;i++) { fread( &amp;physical[i][j][k].color , sizeof(physical[i][j][k].color) , 1 , p ) ; fread( &amp;physical[i][j][k].temperature , sizeof(physical[i][j][k].temperature) , 1 , q ) ; fread( &amp;physical[i][j][k].Bx , sizeof(physical[i][j][k].Bx) , 1 , r ) ; fread( &amp;physical[i][j][k].By , sizeof(physical[i][j][k].By) , 1 , s ) ; fread( &amp;physical[i][j][k].Bz , sizeof(physical[i][j][k].Bz) , 1 , t ) ; } } } fclose(p); fclose(q); fclose(r); fclose(s); fclose(t); </code></pre> <p>I just need to know whether I am going about this the right way.....thanks!!</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.
 

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