0

I can find the beginning of the 50 files. I am creating the files and I am writing to them.I suspect things go wrong after the first 512 bytes. When I open in hexcode I cannot see the signature of a jpg so I suppose it is somehow overwritten. I just don't see what is wrong with my code. I chose "a" instead of "w" so that I can append and I'm not getting any error message. When I try to open a file I get "unsupported format"(no signature). What is that I do wrong? (I got five out of six criteria right with a previous version but I would like to understand what I'm doing, this trial and error thing is driving me crazy).

``

 typedef uint8_t  BYTE;

 int main(int argc, char *argv[]) 
 {
 if(argc != 2) {
    fprintf(stderr, "Please enter one single file name");
    return 1;
 }

 char *fotoFile = argv[1];

 BYTE writeImage[512] = {0};
 char name[7];
 int tekst = 0;    

 FILE* inptr = fopen(fotoFile, "r");
 if (inptr == NULL)
 {
    fprintf(stderr, "Could not open %s.\n", fotoFile);
    return 2;
 }

 FILE* outptr = fopen(name, "a");  
 if (outptr == NULL)
 {
    fclose(inptr);
    fprintf(stderr, "Could not create %s.\n", name);
    return 3;
 }    

 int stop = 0;
 int einde = 0;

 while(einde == 0) {

    fread(writeImage, 512, 1, inptr);

    if(!feof(inptr)) {             

        if (writeImage[0] == 0xFF && writeImage[1] == 0xd8 && writeImage[2] == 0xFF) 
        {
            if(writeImage[3] > 223 && writeImage[3] < 240) 
            {   
                sprintf(name, "%03i.jpg", tekst);
                tekst++;   
                fclose(outptr);
                outptr = fopen(name, "a");
                fwrite(writeImage, 512, 1, outptr);                    
                if (stop == 0) 
                { 
                    stop = 1; 
                }    
            }
        }
        if(stop == 1) 
        {
           if (writeImage[0] != 0xFF && writeImage[1] != 0xd8 && writeImage[2] != 0xFF) 
            {
                if(writeImage[3] < 224 || writeImage[3] > 239) 
                {
                    fwrite(writeImage, 512, 1, outptr);
                }
            }    
        } 
    }
    if(feof(inptr)) 
    {
        einde = 1;
        fclose(inptr);        
    }    
}

}``

1 Answer 1

1

Let me guess, after it is written out to the output file, the first byte of the signature is 0x00? A very subtle but common problem. Look at the following:

 char name[7];
 ...
        sprintf(name, "%03i.jpg", tekst);

How many chars are you writing to name? Did you allow for the end of string marker in that count????? Since name is too short by 1, the \0 end of string marker is written into the next physical byte in memory, beyond what was allocated for the name array. Commonly, this turns out to be the first byte of the buffer, so the first byte of the signature gets overwritten with binary 0's. Sound familiar?

Next, let's look at your tests later on....

       if (writeImage[0] != 0xFF && writeImage[1] != 0xd8 && writeImage[2] != 0xFF) 

In complex logic, applying != where there were == operators does not reverse the logic of the original test. Simply put, if any one of the first 3 bytes matches a signature byte, this test will fail. The problem here is that this should fail ONLY if all three don't match. If less than 3 of them match, it needs to pass.

When you want to reverse a test, the simplest and correct way to do it is to take the original test, enclose it in parentheses, and put the not operator, ! in front of it, as in !(complex_test). There are other ways to do it, but it's about a week's worth of lectures in a digital logic class to explain it. ;-)

There may be more issues (I think I see at least 1 unrelated to your question), but this will get you going again. Happy coding!

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

2
  • Yes 0x00 sure sounds familiar and I have been fooling with that condition changing all the && to ||, only that didn't work either. I thought I was being subjected to a psychological experiment testing just how far I could be driven. I was seeing nasty little midgets with triangular faces trying to get into my webcam. You saved me just in time. Thank you very much for both tips.
    – Karin
    Commented Jun 14, 2017 at 11:56
  • Glad your sanity has been saved. If you're happy, how about accepting the answer so that it gets removed from the unanswered question pool? ;-)
    – Cliff B
    Commented Jun 14, 2017 at 16:40

You must log in to answer this question.

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