0

I am trying to ease into this pset and am having difficulty starting with the first step - locate 1st jpg, create and write. I think that once I have that figured out, I'll have an easier time building out the rest.

1) My code compiles, but produces nothing. 2) I think (I think) that I figured out the difference between sprint (create jpg) and fwrite (write jpg to file). 3) I think that I correctly open and close the card.raw and the new jpg 4) Based on Zamyla's directive, I think I have the order of operation in the loop Lots of thinking, but no certainties:) If anyone can advise on whether or not I'm on the right track, it'd be great. Thanks!

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

typedef uint8_t  BYTE;
BYTE buffer[512];
int counter= 0;


int main(int argc, char* argv[])
{    
    FILE* inptr = fopen("card raw", "r");
    if (inptr == NULL)
    {
      printf("Could not open.\n");
      return 2;
    }

    char write_jpg[4];
    int counter=0;
    FILE* jpg = fopen(write_jpg, "w");

    while(fread(&buffer,512,1,inptr) != 0)
   {
    fread(&buffer,512,1,inptr);

    if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
    {
        sprintf(write_jpg,"%03i.jpg",counter);
        jpg = fopen(write_jpg, "w");
        fwrite(&buffer, 512, 1, jpg);
        fclose(jpg);
        counter++;
    }
}   
fclose(inptr); 
}
0

There are a number of problems. First, you have a typo in fopen("card raw", "r"); You have a space in the filename where there should be a period.

char write_jpg[4]; should be 8, not 4. It's too short to hold the complete filename, so it seg faults.

char write_jpg[4];
int counter=0;
FILE* jpg = fopen(write_jpg, "w");

These three lines declare jpg and open the first output file, but since write_jpg was not actually initialized, the filename is whatever garbage data the system finds at that address, and ends when the system finds some random data that is equal to the end of string marker of \0. You would have done better to set jpg = NULL here.

Next, you have two fread() commands back to back, so you're going to skip half of the data.

Finally, although not incorrect, you have declared 3 global variables outside of main. This is generally considered a bad practice and should be avoided unless there's a good reason for doing so.

This should get you started.

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

5
  • !! Thank you!! My code isn't complete yet, but it is doing something now, so now I think I can tinker with it until it works properly. :) – Tony Burroughs Oct 10 '15 at 14:29
  • Quick question - what is the logic behind setting the initial value of jpg to NULL? I thought that NULL is cause for code to break. – Tony Burroughs Oct 10 '15 at 14:33
  • No, setting to NULL isn't a cause for code to break. However, trying to do something like reading a file when the poiter is set to NULL will usually cause problems. The reason for setting the file pointer to NULL is this: when you create a file pointer, the contents of the pointer will just be whatever was left in memory. Unless you initialize it to point to a real file, or to NULL, there's no way for code to tell that the data stored in the pointer is valid. NULL is a special value that says "Hey, this poiter doesn't point anywhere." (continues....) – Cliff B Oct 10 '15 at 16:24
  • With that in mind, if you have definitely initialized a pointer to point at something real, you can use it. However, if your code needs to do something with a pointer and you don't know for sure that it has been initialized, you can and should test to see ` if( pointer == NULL)` before doing anything with it. This logic applies to any situation or var that can be set to NULL, not just file pointers. – Cliff B Oct 10 '15 at 16:27
  • ahh, I understand. Thanks again for your explanation:) – Tony Burroughs Oct 11 '15 at 3:06

You must log in to answer this question.

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