Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing and nested pointers
    text
    copied!<p>I am trying to write a program that reads a file containing tetrahedrons formatted as such:</p> <pre><code> T 109 20 10 19 [T X1 X2 X3 X4 ] T 123 34 24 33 T 182 83 82 92 </code></pre> <p>Where each number X in each row denotes a triplet (of x, y and z coordinates) at line X of a data file :</p> <pre><code>line 1 x y z line 2 x y z [.......] line n x y z </code></pre> <p>I have posted a similar (but simpler) question here <a href="https://stackoverflow.com/questions/14729035/parsing-data-from-ascii-formated-file-in-c">Parsing data from ASCII formatted file in C</a> . This time round my idea is to read the tetrahedron file, and store the 4 line numbers representing the coordinates of the vertices of these tetrahedrons in an array <code>vertid[]</code> (0 through 3) and then linking the tetrahedrons to the vertices using an array of pointers to the <code>Vertex</code> structure. I'll summarise: Store line ID numbers in tetrahedron structure and then set pointers to "point" at the correct Vertex. </p> <p>Please bear in mind that I am no programmer (I need this to generate a Voronoi diagram). Also would it be simpler if instead of storing the vertices that makeup the tetrahedrons, I stored the edges (as I believe I will actually require edge data rather than vertex data for the generation of the Voronoi D) ? I have spent my Sunday working on this and its not working and I haven't got a clue why. Thank you.</p> <p>(not working) example:</p> <pre><code>#define FILEPATHtri "/pathto/grid1DT.txt" #define FILEPATHorg "/pathto/grid1.txt" #define MAX 10000 #include &lt;assert.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include "math.h" typedef struct { float x; float y; float z; char ID; }Vertex; /* NOT USED ATM typedef struct { edge_ref next[4]; // pointers to other edges (AQE data structure) void *data[4]; // pointers to endpoints of edge unsigned ID; // ID of edge }Edge; */ typedef struct { Vertex *vert[4]; int vertid[4]; char t; }Tetra; void file_link (void) { Vertex *vert_dt = malloc( MAX * sizeof (Vertex) ) ; Vertex *vert_std = malloc( MAX * sizeof (Vertex) ) ; Tetra *tetrahedron = malloc(MAX * sizeof (Tetra) ); FILE *fp1,*fp2 ; fp1 = fopen( FILEPATHtri,"r"); int i = 0; while(fscanf(fp1, "%c %i %i %i %i ", &amp;tetrahedron[i].t, &amp;tetrahedron[i].vertid[0], &amp;tetrahedron[i].vertid[1], &amp;tetrahedron[i].vertid[2], &amp;tetrahedron[i].vertid[3] ) == 5 ) // Saving the line numbers into vertid[0..3] { i++; } fclose(fp1); fp2 = fopen( FILEPATHorg,"r"); int j = 0; while(fscanf(fp2, "%f %f %f ", &amp;vert_std[j].x, &amp;vert_std[j].y, &amp;vert_std[j].z ) == 3 ) { j++; } fclose(fp2); int n; for (n=0; n&lt;i; n++) { tetrahedron[n]-&gt;vert[0]=vert_std[ tetrahedron[n].vertid[0] ]; // retriving coords from the standard file and storing tetrahedron[n]-&gt;vert[1]=vert_std[ tetrahedron[n].vertid[1] ]; // them in the vertex pointer array of tretrahedron tetrahedron[n]-&gt;vert[2]=vert_std[ tetrahedron[n].vertid[2] ]; tetrahedron[n]-&gt;vert[3]=vert_std[ tetrahedron[n].vertid[3] ]; } int m = 100; //used to retrieve m'th tetra, so that I can print the tetra that I want,just for checking purposes.. // printing 13 variables in total... printf("These are the 4 coordinates of the vertices of the %i th tetrahedron from the DT file %f %f %f\n%f %f %f\n%f %f %f\n%f %f %f\n", n, tetrahedron[n]-&gt;vert[0]-&gt;x,tetrahedron[n]-&gt;vert[0]-&gt;y,tetrahedron[n]-&gt;vert[0]-&gt;z, tetrahedron[n]-&gt;vert[1]-&gt;x,tetrahedron[n]-&gt;vert[1]-&gt;y,tetrahedron[n]-&gt;vert[1]-&gt;z, tetrahedron[n]-&gt;vert[2]-&gt;x,tetrahedron[n]-&gt;vert[2]-&gt;y,tetrahedron[n]-&gt;vert[2]-&gt;z, tetrahedron[n]-&gt;vert[3]-&gt;x,tetrahedron[n]-&gt;vert[3]-&gt;y,tetrahedron[n]-&gt;vert[3]-&gt;z); free(vert_dt); free(vert_std); free(tetrahedron); } int main(void) { file_link(); return 0; } </code></pre>
 

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