title: Copying content from one file to another, segmentation fault
body: <p>I'm trying to copy 50 jpegs, one by one from a large .raw file, however currently I get a segmentation fault error. Here's my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdint.h&gt;
  
  typedef uint8_t BYTE;
  
  //SOI - 0xFF 0xD8
  //EOI - 0xFF 0xD9
  //APPn - 0xFF 0xEn    
  int main(void)
  {
      //FAT - 512 bytes per block
      BYTE block[512];
  
      //open file containing pictures
      FILE* card_file = fopen("card.raw", "rd");
  
      FILE* jpeg_file;
  
      //make sure the file opened without errors
      if (card_file == NULL)
      {
          printf("something went wrong and file could not be opened");
          return 1;
      }
  
      int i = 0;
      while (fread(&amp;block, sizeof(BYTE), 512, card_file) != 0)
      {
          //jpeg start signature
          if(block[0] == 0xFF &amp;&amp; block[1] == 0xD8)
          {
              i++;
              if(jpeg_file != NULL)
                  fclose(jpeg_file);
  
              //create a new jpeg file to copy bytes to
              jpeg_file = fopen((char*)i, "w+");
          }
  
          //write 512 bytes to a jpeg file
          if(jpeg_file != NULL)
              fwrite(block, sizeof(block), 1, jpeg_file);
      }
  
      fclose(card_file);
  return 0;
  }
  </code></pre>
  
  <p>when I run it through GDB, my code gets all the way to if(block[0] == 0xFF &amp;&amp; block<a href="https://i.stack.imgur.com/XAPq0.jpg" rel="nofollow noreferrer">1</a> == 0xD8), then it skips the condition and segmentation fault occurs. I don't see what might be causing this.
  Here's a screenshot:
  <img src="https://i.stack.imgur.com/XAPq0.jpg" alt="enter image description here"></p>
  
  <p>Code updated:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;cs50.h&gt;
  
  typedef uint8_t BYTE;
  
  /*struct jpg*/
  /*{*/
  /*    BYTE soi[2] = { 0xFF, 0xD8 };*/
  /*    BYTE eoi[2] = { 0xFF, 0xD9 };*/
  /*};*/
  
  //SOI - 0xFF 0xD8
  //EOI - 0xFF 0xD9
  //APPn - 0xFF 0xEn    
  int main(void)
  {
      //FAT - 512 bytes per block
      BYTE block[512];
  
      //jpeg name
      char name[6];
  
      bool is_open = false;
  
      //JPEG
      //struct jpg image;
  
      //open file containing pictures
      FILE* card_file = fopen("card.raw", "r");
  
      FILE* jpeg_file;
  
      //make sure the file opened without errors
      if (card_file == NULL)
      {
          printf("something went wrong and file could not be opened");
          return 1;
      }
  
      int i = 0;
      while (fread(block, sizeof(BYTE), 512, card_file) != 0)
      {
          //jpeg start signature
          if ((block[0] == 0xFF) &amp;&amp; (block[1] == 0xD8) &amp;&amp; (block[2] == 0xFF) &amp;&amp; ((block[3] == 0xe1) || (block[3] == 0xe0)))
          {
              //assign jpeg name
              sprintf(name, "%d.jpg", i++);
              if(is_open)
                  fclose(jpeg_file);
  
              //create a new jpeg file to copy bytes to
              jpeg_file = fopen(name, "a+");
              is_open = true;
          }
  
          //write 512 bytes to a jpeg file
          if(is_open)
              fwrite(block, sizeof(block), 1, jpeg_file);
      }
  
      fclose(jpeg_file);
      fclose(card_file);
  return 0;
  }
  </code></pre>
  
  <p>Now it doesn't crash, however only 9 out of 50 jpegs are properly recovered. cs50.h is there just so I have access to bool type. What's a better way to write 50 files? I seem to have a logical flaw with my booleans.</p>