0

I have had some segmentation fault and errors in this code before that was fixed with you guys help (thanks Cliff B!). After fixing everything that I could, and thinking that everything now makes sense to me, my program still doesn't give me any jpgs! When I actually compiles the program, nothing actually shows up. I have used gdb to try to check it but it does seem to run properly so I have no clue what's going on. Can someone please help me point out what I'm doing wrong?

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

int main(int argc, char* argv[])
{
// open memory file
FILE* inptr = fopen("card.raw", "r");

//assigning stuff
char jpgname [8];
int c = 0;
uint8_t buffer[512];
int b = 0;

if (inptr == NULL)
{
printf("Could not open file");
return 1;
}   

FILE * outptr = NULL;


while(fread(buffer, 512, 1, inptr) == 1)
{


if( (buffer[0] == 0xff && buffer [1] == 0xd8 && buffer [2] == 0xff && buffer [3] == 0xe0 )|| (buffer[0] == 0xff && buffer [1] == 0xd8 && buffer [2] == 0xff && buffer [3] == 0xe1 ))

{

if (outptr != NULL)
        {
                fclose(outptr);
        }

sprintf (jpgname, "%d.jpg", c);

FILE * outptr = fopen(jpgname, "w");

fwrite (&buffer,512,1,outptr);


b++;
}

else if(b>0)

{
fwrite (&buffer,512,1,outptr);
}


return 0;
}

}
1

You have a number of issues within your code.

  1. output file name format. Your code will create filenames with only one or two digits in the filename. The spec calls for 3, with leading zeros.
  2. Your file name numbering isn't incrementing. Look at the code that generates the name and then what variable is being incremented.
  3. Your program has a flaw that causes it to only read one 512 byte block before exiting. That's the hint, but I'm going to let you track it down. (fix this first!)
  4. You declare outptr twice. The second declaration creates a shadow variable that is the one used to create the output file, but goes out of scope when you try to write the second block to the file. At that point, the original declaration, which points to NULL, is active.

Other than all this, it works fine. ;-)

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

1
  • OMG I GOT IT!!!!! Thanks Cliff B!!!!! – Leon Dec 6 '15 at 23:25

You must log in to answer this question.

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