title: Improving C code to remove copy-paste
body: <p>P.S. Some people had issues understanding the question 'how to improve my code'. I couldn't have made it clearer, really. I solved the issue in the meantime.</p>
  
  <p>I'm working on an assignment to recover photos from a cd card file (.raw) by first finding them by their 'magic numbers'. It's a part of a CS50 class if anybody knows what it is.</p>
  
  <p>Anyway, it seems to work ok-ish. I can recover all photos, but some of them of a worse quality, then others (a lot of white pixels) - not sure if it's intended.</p>
  
  <p>In the code depending on conditions I open and close jpg files to write info into them. I can't figure out a way to avoid copy-paste to fopen, fwrite and fclose the file. I'd like to improve this part. Here's the code:</p>
  
  <pre><code>typedef uint8_t BYTE;
  
  int main(void)
  {
      // open input file 
      FILE* card_file = fopen("card.raw", "r");
      if (card_file == NULL)
      {
          printf("Could not open card.raw\n");
          return 1;
      }
  
  // declare output file
  FILE* image;
  
  // array to store values we read for each 512 bytes
  BYTE buffer[512];
  
  // number of jpg files found
  int jpgs = 0;
  
  // variable used to create new files
  char title[10];
  
  // execute until the end of file is reached
  while(fread(&amp;buffer, sizeof(BYTE), 512, card_file) &gt; 0)
  {
      // find a new jpg file by its magic numbers
      if((buffer[0] == 0xff) &amp;&amp; (buffer[1] == 0xd8) &amp;&amp; (buffer[2] == 0xff))
      {
          //create a new file if it's found
          sprintf(title, "%03d.jpg", jpgs);
          jpgs++;
  
          //open the file and check if file is opened correctly
          image = fopen(title, "a");
  
          if (image == NULL)
          {
              fprintf(stderr, "Could not create %03d.jpg\n", jpgs);
              return 2;
          }
  
          // write into the file and close it
          fwrite(&amp;buffer, sizeof(BYTE), 512, image);
          fclose(image);
      }
      else 
      {
          //check if a jpg file was already found before
          if(jpgs &gt; 0)
          {
              //open the file and check if file is opened correctly
              image = fopen(title, "a");
  
              if (image == NULL)
              {
                  fprintf(stderr, "Could not create %03d.jpg\n", jpgs);
                  return 2;
              }
  
              // write into the file and close it
              fwrite(&amp;buffer, sizeof(BYTE), 512, image);
              fclose(image);
          }
      }
  }
  
  //close the input file
  fclose(card_file);
  
  }
  </code></pre>
  
  <p>I tried to create a separate function, but couldn't figure out how to handle pointer to integer values I need to pass to it.</p>
  
  <p>Any help would be appreciated.</p>