Home > Mobile >  Finding a specific byte in a file
Finding a specific byte in a file

Time:11-24

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:

aÿØÿÿà1234

output:

aÿØÿÿà1234
0

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

CodePudding user response:

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("X %c\n", (unsigned)bytes, 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);
}
  • Related