Note that there are some explanatory texts on larger screens.

plurals
  1. PORead a file into an array of struct's, then sort
    text
    copied!<p>EDIT: Ok, so with some help from the post below me, I got it to compile. I changed the lines that read the file into the array to this:</p> <pre><code> input-&gt;read((char*)( &amp;(zip[i].postalCode) ), sizeof(int )); input-&gt;read((char*)( &amp;junk ), sizeof(int )); input-&gt;read((char*)( &amp;(zip[i].longitude) ), sizeof(double )); input-&gt;read((char*)( &amp;(zip[i].latitude) ), sizeof(double )); cout &lt;&lt; "Currently at position" &lt;&lt; input-&gt;tellg() &lt;&lt; endl; </code></pre> <p>Now I get it to run through the first iteration of the loop, but then get a segmentation fault. </p> <pre><code> Currently at position24 Segmentation fault (core dumped) </code></pre> <p>ORIGINAL POST BELOW:</p> <p>I'm back at it again. A have a struct and a couple functions:</p> <pre><code> struct zipType{ int postalCode; double longitude; double latitude; }; void zipToCout(zipType zip){ cout &lt;&lt; "Postal Code = " &lt;&lt; zip.postalCode &lt;&lt; "\tLongitude = " &lt;&lt; zip.longitude &lt;&lt; "\t\tLatitude = " &lt;&lt; zip.latitude &lt;&lt; endl; } void binRead(zipType *zip[], fstream *input){ int junk; int count; int end; input-&gt;seekg(0,ios::end); end=input-&gt;tellg(); count=input-&gt;tellg()/24; input-&gt;seekg(0); while(input-&gt;tellg()!=end){ for(int i=0;i&lt;count;i++){ input-&gt;read((char*)( &amp;zip[i]-&gt;postalCode ), sizeof(int )); input-&gt;read((char*)( &amp;junk ), sizeof(int )); input-&gt;read((char*)( &amp;zip[i]-&gt;longitude ), sizeof(double )); input-&gt;read((char*)( &amp;zip[i]-&gt;latitude ), sizeof(double )); cout &lt;&lt; "Currently at position" &lt;&lt; input-&gt;tellg() &lt;&lt; endl; //zipToCout(*zip); } } } </code></pre> <p>Now in the main I created a <code>zipType[n]</code> where n is the number of zipTypes in the .bin file. I then called <code>binRead(&amp;testZipType[n],&amp;testFile);</code>. This gives me some errors as you can imagine. I get a few errors similar to this:</p> <pre><code> zipType.cpp:22:32: error: base operand of ‘-&gt;’ has non-pointer type ‘zipType’ input-&gt;read((char*)( &amp;zip[i]-&gt;postalCode ), sizeof(int )); </code></pre> <p>What I need to do, is to go through the file and store each portion of the struct in its appropriate portion of the array. I'm trying to make as little changes as possible to make this work. I know some of you can *&lt;@LD(#) your way to glory, but I'm not there yet and want to be able to read my code. Also, i need to do a qsort on the array to sort it in ascending order (I haven't tried this yet and if you can help me with the first part I'll give it a shot myself.) Thanks for any help! Hopefully soon I'll be able to give back a little around here!</p>
 

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