cs50 pset4 recover.c need advice -


i use guidance on pset cs50. below code have far. stuck @ point have no idea how proceed. when run program output 16 jpg files cannot view them must wrong. tips welcome.

#include <stdio.h> #include <stdlib.h>  int main(void) {                // create buffer store in unsigned char buffer[512];  // array filename 8 "000.jpg" char jpgname[8];  // declaring counter amount of jpegs int jpgcounter = 0;  // open memory card file file* inptr = fopen("card.raw", "r"); file* outptr = null;  if (inptr == null) {     printf("could not open %s.\n", "card.raw");     return 1; }         // repeat until end of card      while(fread(buffer, sizeof(buffer), 1, inptr) == 1)     {                                             // find beginning of jpg         if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))         {                // if allready outptr file open close             if (outptr != null)             {                     fclose(outptr);             }              // create filename jpegs             sprintf(jpgname,"%03d.jpg", jpgcounter);              // open new file name defined sprintf             file* outptr = fopen(jpgname, "w");                                           // write outfile buffer             fwrite(&buffer, sizeof(buffer), 1, outptr);              // increment counter jpegs             jpgcounter++;            }                                         }     fclose(inptr);     return 0;    } 

if understood task correctly, want this-please see below. hint: planning if not beginning of jpg file?

#include <stdio.h> #include <stdlib.h>  int main(void) {                 unsigned char buffer[512] = {0}; char jpgname[8] = {0}; int jpgcounter = 0; file* outptr = null;  // open memory card file file* inptr = fopen("card.raw", "rb"); if (inptr == null) {     printf("could not open %s.\n", "card.raw");     return 1; }     // start reading     while(fread(buffer, sizeof(buffer), 1, inptr) == 1) {                                             // find beginning of jpg         if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))         {                if(null!=outptr)                                {               fclose(outptr);               outptr=null;             }              // create filename jpegs             sprintf(jpgname,"%03d.jpg", jpgcounter);              // open new file name defined sprintf             outptr = fopen(jpgname, "wb");                      if (null == outptr)             {               printf("could not open %s.\n", jpgname);               return 1;             }                                   // write outfile buffer             fwrite(buffer, sizeof(buffer), 1, outptr);              // increment counter jpegs             jpgcounter++;          }           else           {              // continue writing old file if not beginning of file              if(null!=outptr)                 fwrite(buffer, sizeof(buffer), 1, outptr);                                        }        }      if(null!=outptr)       fclose(outptr);     if(null!=inptr)       fclose(inptr);      return 0;    } 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -