0

enter image description hereI'm having trouble when creating the JPG files. The amount of images created and the name is fine, but the content of the image is wrong. Here is my code:

int main(int argc, char *argv[])
{
    if( argc == 2 ) {

        unsigned char * buffer = 0;

        // Open memory card
        FILE *memory_card;
        memory_card= fopen(argv[1], "r");

        if(memory_card == NULL)
        {
            perror("Error: could not open card.raw for reading.\n");
            exit(1);
        }

        int memoryChunk = 512;
        buffer = malloc(memoryChunk);

        int indexOfImage = -1;
        char filename[8];

        // read memory card
        while(fread(buffer, memoryChunk, 1, memory_card))
        {
            if(buffer[0] == 0xff &&
               buffer[1] == 0xd8 &&
               buffer[2] == 0xff &&
               (buffer[3] & 0xf0) == 0xe0)
               {
                printf("encontro un jpg\n");
                indexOfImage++;

                if(indexOfImage <= 9) {
                    sprintf(filename, "00%i.jpg", indexOfImage);
                    // printf("name of jpeg is: 00%i.jpg\n", indexOfImage);
                    // printf("%s", filename);
                } else {
                    sprintf(filename, "0%i.jpg", indexOfImage);
                    //printf("name of jpeg is: 0%i.jpg\n", indexOfImage);
                }
            }
            FILE *img = fopen(filename, "w");

            fwrite(buffer, memoryChunk, sizeof(memoryChunk), img);

        }
        free(buffer);
        fclose(memory_card);
        return 0;
    } else {
        printf("Prompt error, please try again entering two arguments\n");
        return 1;
    }
}

Thanks!

4
  • What is wrong with the content? Can you edit in the out up so we can visualize better the error? – Christopher Holder Nov 26 '19 at 14:09
  • The content works, but the output are jpg broken images. I think the problem is in the writing function... – Hernan Garzon Nov 26 '19 at 14:17
  • can you upload a broken image so we can see it? – Christopher Holder Nov 26 '19 at 14:18
  • i.stack.imgur.com/JHINF.jpg – Hernan Garzon Nov 26 '19 at 14:25
1

You should not fopen(filename, "w") every time you fread(buffer, memoryChunk, 1, memory_card). You should do it only when you closed the previous "output_file". You are rewriting your current output file every cycle with a new chunk of data. And every file stores only last chunk since previous are overwritten.

4
  • mmm, thanks but in this way I get the error "Segmentation fault". – Hernan Garzon Nov 27 '19 at 17:21
  • I've compiled again the program and I've images with more than 3.5mb (?) – Hernan Garzon Nov 27 '19 at 20:00
  • You should ask new questions in new thread, not in comments - nobody see how you changed your code. The logic is: you open outputfile and read chunks from input file, and you write them to output file, until you encounter end-of-file marker. Then you close output file, open new output file and proceed. – Rustam A. Nov 28 '19 at 12:27
  • Thanks a lot! I finally was able to resolve it :) – Hernan Garzon Nov 28 '19 at 13:05

You must log in to answer this question.

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