0

i thought i got the main idea with my pseudocode and actually it opens the exact amount of files in the "card.raw" but when i try to open them none of them opens and gives "invalid or unsupported Image format" error.Can someone help me to spot what am i doing wrong ?

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

typedef uint8_t  BYTE;

int main(int argc, char* argv[])
{
// global variable to count array for title
int jpg = 0;
FILE* img = NULL;

// open card file
FILE* inptr = fopen("card.raw", "r");
if (inptr == NULL)
{
    printf("couldn't open the file\n");
    return 1;
}

// temporary storage for buffering
BYTE buffer[512];

// repeat until end of card file
while (!feof(inptr)) 
{
    // read 512 bytes into a buffer
    fread(&buffer, 512 * sizeof(BYTE), 1, inptr);

    // check if its a start of a new jpg
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
    {
        // check if there is an image file open to close it
        if (img != NULL)
        {
            fclose(img);
        }
        // temporary storage for title buffer
        char title[3];

        // make title names for each new jpg
        sprintf(title, "%.3d.jpg", jpg);

        // open a new file 
        img = fopen(title, "a");
        if (img == NULL)
        {
            printf("couldn't open the new image file\n");
            return 2;
        }
        //update the counter
        jpg++;
    }

    // check if its already a jpg
    if (img != NULL)
    {
        //write to the newfile
        fwrite(&buffer, 512 * sizeof(BYTE), 1, img);
    }
}
//close the files
fclose(img);
fclose(inptr);
return 0;

}

1

You are declaring title with length 3, which will hold 2 chars and the end of string marker. You then try to stuff a 7 character filename in it. This is messing up the filenames. title needs to be sufficiently long to correctly hold the file name string plus the end of string marker.

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

2
  • thanks a lot it worked but now my only problem is even if i had the 16 images looking correct the last one of them is giving error on check50. – Erkan Apaydın Dec 8 '15 at 9:23
  • ok i made it work totally after deleted fread line in the loop and changed the whi condition to that fread line equals one. – Erkan Apaydın Dec 8 '15 at 10:30

You must log in to answer this question.

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