2

I've been staring at this for days and can't seem to find what's wrong. My code compiles, makes a file for the JPG's found, only doesn't show the JPG's but a weird screen full of symbols in white and red. I feel like I'm pretty close, but I can't find what I did wrong. I've also added part of the output. Please help!

    // declare all variables
    int pictures = 0;
    FILE* img = NULL;
    int imgopen = 0;
    char filename[10];

        {
            //first JPG
            if (imgopen == 0)
            {
                sprintf(filename, "%03d.jpg", pictures);
                img = fopen ("filename", "w");
                fwrite (buffer, 512, 1, img);
                imgopen++;
            }

Out of academic honesty, I've removed the irrelevant pieces of my code, and only left the part where I made a mistake.

2

I see two things that could be causing problems:

1.- You are calling your jpgs literally "filename". If you call a file "filename" it will open as a text file. You are seeing the character representations of all the bytes in the image.

I suggest you refer to your variable filename without the quotes on lines 47 and 57. Something like this:

img = fopen (filename, "w");

2.- You should check for all of these bytes for temp[3]:

0xe0
0xe1
0xe2
0xe3
0xe4
0xe5
0xe6
0xe7
0xe8
0xe9
0xea
0xeb
0xec
0xed
0xee
0xef

Maybe you should change your last piece of the condition on line 41 from:

(temp[3] == 0xe0 || temp[3] == 0xe1)

to something like this:

(temp[3] >= 0xe0 || temp[3] <= 0xef)
| improve this answer | |
  • Thank you so much! This helped me a lot, it works exactly as it's supposed to now. – Petje Oct 21 '16 at 19:08
  • Thanks to you! : ) Nice question with images and all. – Ricardo David Antonetti Oct 21 '16 at 19:44

You must log in to answer this question.

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