0

"###.jpg" gets created from 000-049(all 50 files are created in the jpg folder) but the images 7, 11, 13, 14 can't be opened and the image 12 shows only random lines. Image 15 is also not perfect like other photos. The below is my implementation of code.

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

int main(void)
{
    FILE* fp;
    fp = fopen("card.raw", "r");

    if (fp == NULL)
    {
        printf("Could not open card.raw.\n");
        return 2;
    }
    uint8_t data[512];
    char titles[7][50];

    int j = 0;
    FILE* jpgFile = NULL;

    while(1==1){

        if(feof(fp)){
            if (jpgFile != NULL){
                fclose(jpgFile);
            }
            return 0;
        }
        fread(data, sizeof(int8_t) * 512, 1, fp);
        if (((data[0] == 0xff) && (data[1] == 0xd8) && (data[2] == 0xff) && (data[3] >= 0xe0 && data[3] <= 0xef))){

            sprintf(titles[j], "%03i.jpg", j);

            if (jpgFile != NULL){
                fclose(jpgFile);
            }
            jpgFile = fopen(titles[j], "a");
            j++;

            if(jpgFile != NULL){
                fwrite(data, sizeof(int8_t) * 512, 1, jpgFile);
            }
        }
        else {
            if (jpgFile != NULL){
                fwrite(data, sizeof(int8_t) * 512, 1, jpgFile);
            }
        } 
    }
    if (jpgFile!=NULL){
        fclose(jpgFile);
    }
    return 0;
}

I think that I'am missing something in code. Any help will be appreciated.

0

The problem is well hidden in plain sight. char titles[7][50]; This approach is problematic. When you use this 2-d array, the code is storing file names in titles[j], meaning the first dimension, not the second. Since there are only 7, it's going to be writing and reading to parts of memory that have been allocated to other vars in memory, or to memory not allocated to the program. This data overwriting is causing problems with the file creatiion, probably because its overwriting part of the buffer array data.

Next, even if you reverse the dimensions, 7 is too short. It doesn't allow for the end of string marker, \0. It needs to be at least 8.

Instead of a 2-d array to store filenames, why not use a 1-d array large enough to create the current file name, and reuse the array each time?

Following that, the program should really be able to handle an undetermined number of files, not exactly 50. (BTW, did you know that check50 uses different test data and a different number of output files? ;-) )

Think about how the code is opening output files too. It opens the files with "a" or append mode. What happens if the file already exists?

That takes most of the problems asked about. THere's one more lurking in the background, but I'll let you find it on your own. Here's a hint though. How and when is your code actually handling the EOF of the input file?

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

2
  • the code always check for EOF before reading another buffer. if(feof(fp)){ if (jpgFile != NULL){ fclose(jpgFile); } return 0; } but clearly that is not enough as the check50 is showing that the last image is not restored correctly. Sep 10 '16 at 4:37
  • changing the while condition to 'while(fread(data, sizeof(int8_t) * 512, 1, fp)==1)' solved that issue. Sep 10 '16 at 4:43

You must log in to answer this question.

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