Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding a specific byte in a file

I have a file in which I’m trying to look for this sequence of bytes: 0xFF, 0xD8, 0xFF, and 0xE0. For right now, let’s assume I’m only looking for 0xFF. I made this program for testing:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void analyzeFile(char* filename)
{
    FILE* filePtr = fopen(filename, "rb");

    int numImages = 0;

    while (!feof(filePtr))
    {
        char bytes;

        bytes = getc(filePtr);

        printf("%c", bytes);

        if ((bytes == 0xFF))
        {
            numImages++;
            printf("image found!\n");
        }
    }

    printf("%d\n", numImages);
}

This isn’t working. When I call analyzeFile with parameter "test.txt", it prints the contents of the file out fine, but doesn’t detect a single 0xFF byte:

contents of test.txt:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

aÿØÿÿà1234

output:

aÿØÿÿà1234
0

for reference, 0xFF is equivalent to y-diaeresis, ÿ, according to ASCII.

>Solution :

There are two problems with your code. For the first, see: Why is “while ( !feof (file) )” always wrong?

The second problem is that getc (or fgetc) returns an int, not a char. As it stands, your char value of 0xFF is sign-extended (to 0xFFFFFFFF, most likely) when it is promoted to an int for the if ((bytes == 0xFF)) comparison. So, use int for your bytes variable and change the loop to test the value that was read for the EOF signal:

void analyzeFile(char* filename)
{
    FILE* filePtr = fopen(filename, "rb");
    if (!filePtr) { // Add some error handling...
        printf("Could not open file!");
        return;
    }
    int numImages = 0;
    int bytes;
    while ( ( bytes = getc(filePtr) ) != EOF) {
        printf("%02X %c\n", bytes, (char)bytes);

        if (bytes == 0xFF) { // Removed redundant extra parentheses
            numImages++;
            printf("image found!\n");
        }
    }
    fclose(filePtr); // Don't forget to close the file!
    printf("%d\n", numImages);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading