0

typedef uint8_t BYTE; int main(int argc, char* argv[])

{ // TODO // open input file FILE* inptr = fopen("card.raw", "r"); if (inptr == NULL) {

    printf("Could not open.\n");

    return 1;
}
// temporary block reading
 BYTE buffer[512];
// found jpgs name and number   
int file_num = 0;
char file_name[8];
// open first out file
char* outfile = NULL;
FILE* outptr = fopen(outfile, "w");
if (outptr == NULL)
{       
    fclose(inptr);
    fprintf(stderr, "Could not create %s.\n", outfile);
    return 2;
}
// reading to the file end

while (!feof(inptr))
{
    // read first block
    fread(&buffer, sizeof(BYTE), 512, inptr);
    // check jpg sequences
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
    {
        while (!feof(inptr)) // after getting the first jpg read to the end
        {
            fwrite(&buffer, sizeof(BYTE), 512, outptr);
            fread(&buffer, sizeof(BYTE), 512, inptr);
            // ckeck 
            if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
             {
                 // close and name the file
                 fclose(outptr);
                 sprintf(file_name, "%03d.jpg", file_num);
                 file_num++;
                 // open new out file
                char* outfile = NULL;
                FILE* outptr = fopen(outfile, "w");
                if (outptr == NULL)
                {
                    fclose(inptr);
                    fprintf(stderr, "Could not create %s.\n", outfile);
                     return 2;
                 }
            }
        }
    }
} 
return 0;

}

1 Answer 1

0

According to the man page for fopen():

FILE *fopen(const char *path, const char *mode);

...
...

The fopen() function opens the file whose name is the string pointed to by path.....

char* outfile = NULL; creates outfile as a string variable, however it has "no name" so the fopen() command fails.

1
  • i get rid of one of while loops and correct the fopen. it works perfectly. thanks a lot Dec 18, 2015 at 7:17

You must log in to answer this question.

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