title: correct way to copy binary file to buffer in c
body: <p>I'm trying to open a bin file an get its data into a buffer and copy the data from that buffer to a section that I made called .my_data.
  When I do hexdump binfile.bin I see</p>
  <pre><code>   00000000 4455 e589 00bf 0000 e800 0000 00b8
     00000010 5d00 00c3
     00000015
  </code></pre>
  <p>and when I printf the buffer and buffer2 I see</p>
  <pre><code>   55 48 89 e5 bf 00 00 00 00 e8 00 00 00 00 b8 00 00 00 00 5d c3 
  
     55 48 89 e5 bf 00 00 00 00 e8 00 00 00 00 b8 00 00 00 00 5d c3
  </code></pre>
  <p>which matches the output from hexdump. However when load buffer2 into my new section (I didn't include that part of the code), I used objdump -s -j .my_data foo and I see some other data not similar to the output above.</p>
  <p>I apologize for the dumb question but I just want to confirm if the memcpy I used it's actually copying the data from buffer to buffer2 so I can discard this part and look for the error in the rest of my program. From the output of the second for loop I would think it is. I'm also using gcc, thanks.</p>
  <pre><code>   #include &lt;stdio.h&gt;
     #include &lt;stdint.h&gt;
     #include &lt;string.h&gt;
     #include &lt;stdlib.h&gt;
     
     int main (){
  
     uint8_t buffer[21];
     uint8_t buffer2[21];
     FILE *ptr;
     ptr = fopen(&quot;binfile.bin&quot;, &quot;rb&quot;);
     size_t file_size = 0;
     
     if(!ptr)
     {
       printf(&quot;unable to open file&quot;);
       return 1;
     }
  
     fseek(ptr, 0, SEEK_END);
     file_size = ftell(ptr); 
     rewind(ptr);
  
     int i = 0;
     
     fread(buffer, sizeof(buffer), 1, ptr);
     memcpy(buffer2, buffer, sizeof(buffer));
  
     for(i = 0; i &lt; 21; i++)
     {
      printf(&quot;%02x &quot;, buffer[i]);
     }
  
     printf(&quot;\n\n&quot;);
     for(i = 0; i &lt; 21; i++)
     {
      printf(&quot;%02x &quot;, buffer2[i]);
     }
  
     fclose(ptr);
    
     return 0;
  
     }
  
      
  </code></pre>