0

Looked at some of the other posts on this issue. For me it seems like I'm taking the correct size of (not buffer pointer but actual buffer). Can't seem to figure out why this is failing check50. All images are generated correctly. Any help would be appreciated!

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

unsigned char buffer[512];
unsigned char buffer1[512];

int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 2)
  {
      fprintf(stderr, "Usage: ./copy infile outfile\n");
      return 1;
  }

char *infile = argv[1];

// Open input / card file

FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
       fprintf(stderr, "Could not open %s.\n", infile);
       return 2;
}

// set file_counter to 0 and intialize counter array
char title[8];
int file_counter = 0;
sprintf(title,"%03d.jpg", file_counter);
FILE *outptr = fopen(title, "w");

//Repeat until end of card
//for (int a = 0; a <50000; a++)
while (feof(inptr) ==0)
{


fread(&buffer, sizeof(buffer), 1, inptr);
//check if its the start of new JPEG file
if  (buffer[0] == 0xff &&
     buffer[1] == 0xd8 &&
     buffer[2] == 0xff &&
     (buffer[3] & 0xf0) == 0xe0)
    {
    //open and write to a new file
    file_counter = file_counter + 1;
    if (file_counter == 1)
        {
        fwrite(&buffer, sizeof(buffer), 1, outptr);
        }
    else
        {
    fclose(outptr);
    sprintf(title,"%03d.jpg", file_counter);
    outptr = fopen(title, "w");
    fwrite(&buffer, sizeof(buffer), 1, outptr);
        }
    }

    else
    //if its not the start of a new JPEG file
    {
        //have you already found a jpeg? then add to it
        if (file_counter > 0)
            {
            //already found a jpeg?
              fwrite(&buffer, sizeof(buffer), 1, outptr);
            }
        //just spacers before?
        else
            {
            }
    }
    }

    fclose(inptr);
     }
0

Here's a really big clue. Have you looked at file 001.jpg? EXACTLY how many files are being created and what should the name of the last file be?

Remember to run rm *.jpg at the command line before running your program.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

8
  • so I see that I'm not generating file 1 - I ammended my code to do that but now check50 and submit50 do not seem to work...my code is still generating the images fine though – Arun Narayanan Nov 10 '17 at 1:37
  • Scratch this comment so I'm still getting an error. – Arun Narayanan Nov 10 '17 at 2:37
  • :( recovers 000.jpg correctly 000.jpg not found :( recovers middle images correctly recovered image does not match :( recovers 015.jpg correctly recovered image does not match – Arun Narayanan Nov 10 '17 at 2:38
  • my code it not skipping numbers like before correctly has 001 but it is generated 50 images like its supposed to – Arun Narayanan Nov 10 '17 at 2:38
  • So where is file 000.jpg? – Cliff B Nov 10 '17 at 2:42
0

include

            #include <stdio.h>
            #include <stdlib.h>

            unsigned char buffer[512];
            unsigned char buffer1[512];

            int main(int argc, char *argv[])
            {
                // ensure proper usage
                if (argc != 2)
                  {
                      fprintf(stderr, "Usage: ./copy infile outfile\n");
                      return 1;
                  }

                char *infile = argv[1];

            // Open input / card file

                FILE *inptr = fopen(infile, "r");
                if (inptr == NULL)
                {
                       fprintf(stderr, "Could not open %s.\n", infile);
                       return 2;
                }

            // set file_counter to 0 and intialize counter array
                char title[8];
                int file_counter = 0;
                FILE *outptr;

            //Repeat until end of card
                //for (int a = 0; a <50000; a++)
               while (feof(inptr) ==0)
               {


                fread(&buffer, sizeof(buffer), 1, inptr);
                //check if its the start of new JPEG file
                if  (buffer[0] == 0xff &&
                     buffer[1] == 0xd8 &&
                     buffer[2] == 0xff &&
                     (buffer[3] & 0xf0) == 0xe0)
                    {
                    //open and write to a new file
                    if (file_counter == 0)
                        {
                        file_counter = file_counter + 1;
                        sprintf(title,"%03d.jpg", file_counter);
                        outptr = fopen(title, "w");
                        fwrite(&buffer, sizeof(buffer), 1, outptr);
                        }
                    else
                        {
                    fclose(outptr);
                    file_counter = file_counter + 1;
                    sprintf(title,"%03d.jpg", file_counter);
                    outptr = fopen(title, "w");
                    fwrite(&buffer, sizeof(buffer), 1, outptr);
                        }
                    }

                else
                    //if its not the start of a new JPEG file
                    {
                        //have you already found a jpeg? then add to it
                        if (file_counter > 0)
                            {
                            //already found a jpeg?
                              fwrite(&buffer, sizeof(buffer), 1, outptr);
                            }
                        //just spacers before?
                        else
                            {
                            }
                    }
                }

                fclose(inptr);
            }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .