title: C - Reading data into an array of struct, memcpy?
body: <h3>edit for clarity:</h3>
  <p><code>fscanf</code> is working the way it is written here...it's getting the data into the array that's broken.</p>
  <h3>another edit for clarity:</h3>
  <p>The only point of failure is the <code>memcpy</code> line on the second iteration through the loop where <code>fileindex</code> is 0 and <code>i</code> is 1. the <code>memcpy</code> line works fine when <code>i</code> is 0.</p>
  <hr />
  <p>I'm converting a program that used to read data from a binary file directly into an array using <code>fread</code> into reading from ascii text. Here's the setup:</p>
  <pre><code>#define MAX_FLIGHT_ENTRIES    27000
  #define MAX_PLANES_IN_A_FLIGHT 10
  
  typedef struct {
      double local_x;
      double local_y;
      double local_z;
      float pitch;
      float roll;
      float heading;
      float gearpos;
      float flappos;
      float speedbrakepos;
      float canopypos;
      float afterburnerOn;
      float kias;
      float time;     // record timestamp
  } FLIGHT_ENTRY_TYPE;
  static FLIGHT_ENTRY_TYPE FlightEntries [MAX_PLANES_IN_A_FLIGHT][MAX_FLIGHT_ENTRIES];
  </code></pre>
  <p>The way this used to work is, in a loop, the array would be filled via:</p>
  <pre><code>fread (&amp;FlightEntries[fileIndex][i], sizeof (FLIGHT_ENTRY_TYPE), 1, pFile);
  </code></pre>
  <p>And I believe this would actually instantiate each entry in the array by putting the data directly into memory.  Now I'm reading from a text file and I've tried everything I can to get the values into the array, but it only ever writes the first entry FlightEntries[0][0].  Any attempt to read or write to FlightEntries[0][1] crashes. Here's my current best attempt.</p>
  <pre><code>for ( i = 0; i &lt; MAX_FLIGHT_ENTRIES; i++)
  {               
          // If the file end is found before it should be, set values to defaults
          // and save the file
          if (feof(pFile))
          {
              FlightInfo[fileIndex].endFrameIndex = i - 1;
              break;
          }
          else
          {
              float plocalx, plocaly, plocalz;
              float ppitch, proll, pheading, pgearpos, pflappos, pbrakepos, pcanopypos, pafterburnon, pkias, ptime;
              
              int fresult;
              fresult = fscanf(pFile, &quot;%f %f %f %f %f %f %f %f %f %f %f %f %f\n&quot;,
                  &amp;plocalx,
                  &amp;plocaly,
                  &amp;plocalz,
                  &amp;ppitch,
                  &amp;proll,
                  &amp;pheading,
                  &amp;pgearpos,
                  &amp;pflappos,
                  &amp;pbrakepos,
                  &amp;pcanopypos,
                  &amp;pafterburnon,
                  &amp;pkias,
                  &amp;ptime);
  
                  FLIGHT_ENTRY_TYPE newEntry;
  
              newEntry.local_x = (double)plocalx;
              newEntry.local_y = (double)plocaly;
              newEntry.local_z = (double)plocalz;
              newEntry.pitch = ppitch;
              newEntry.roll = proll;
              newEntry.heading = pheading;
              newEntry.gearpos = pgearpos;
              newEntry.flappos = pflappos;
              newEntry.speedbrakepos = pbrakepos;
              newEntry.canopypos = pcanopypos;
              newEntry.afterburnerOn = pafterburnon;
              newEntry.kias = pkias;
              newEntry.time = ptime;
  
              memcpy (&amp;FlightEntries[fileIndex][i], &amp;newEntry, sizeof FLIGHT_ENTRY_TYPE);
          }
  }
  </code></pre>
  <p>I don't think the array entries are getting allocated properly.  I've tried accessing the individual structure members directly via <code>FlightEntries[fileIndex][i].local_x = (double)plocalx;</code> and I've also tried using <code>memcpy</code> to do the same thing for each member...am I doing my pointers wrong or something?  I have no idea where to go with this. Every time I get past one stumbling block, something else comes up and I think it's all related to the array as opposed to reading from file. Do I have to do something to allocate space?</p>
  <h3>The big question is:</h3>
  <p>What does <code>fread</code> do with binary data in FlightEntries[0][1] that <code>memcpy</code> isn't doing? And is my <code>memcpy</code> line correct? Do I need to do some kind of <code>malloc</code>?</p>