0

In C, I first opened a binary file like this---

FILE *BINfile = fopen("./tmp.bin", "rb+");

Then I transferred the data into an unsigned char array (typedef as byte) like this---

(Side note; I'm an amateur so I'm more comfortable with arrays)

typedef unsigned char byte;
size_t GetFileSize (FILE *file) {
    fseek(file, 0, SEEK_END);
    size_t Size = ftell(file);
    rewind(file);
    return Size;
}

byte BinFileArray[GetFileSize(BINfile)];
size_t BinReadAmount = fread(BinFileArray, 1, GetFileSize(BINfile), BINfile);

Then I wrote a function to find an int value in that bin array by first converting the int to an byte array---

size_t BinaryOffsetFinder (byte Array[], size_t ReadAmount, size_t Target) {
    size_t index=0;
    byte charTarget[sizeof(Target)];
    byte buffer[sizeof(charTarget)];

    memcpy(charTarget, &Target, sizeof(Target));

    for (int i=0; i<ReadAmount; i++) {
        for (int j=0; j<sizeof(buffer); j++) {
            buffer[j]=Array[i+j];
        }

        if (buffer==charTarget) {
            index=i;
            break;
        }
    }
    return index;
}

Then I ran it like this---

printf("\n%d", BinaryOffsetFinder(BinFileArray, BinReadAmount, 5019));
//I know the file has 5019 in it cause I'm modding a binary game file. Verified using ImHex

And I'm pretty confident that a lot of stuff is really wrong. Not because I'm getting 0 every time, but I seriously lack concepts here. Someone please point out the conceptual errors.

18
  • 2
    Questions seeking debugging help should generally provide a minimal reproducible example of the problem, which includes a function main and all #include directives. You are explaining the individual code snippets nicely, so it would be good to keep them. But it would also be good to additionally post a compilable program that reproduces the issue, which other people can test. This program should use the code snippets that you posted. Commented 2 days ago
  • 2
    Turn on your compiler warnings and mind those warnings. Commented 2 days ago
  • 2
    This printf("\n%d", BinaryOffsetFinder(BinFileArray, BinReadAmount, 5019)); is incorrect since the function BinaryOffsetFinder() returns size_t which needs the format specifier %zu not %d. Also as a matter of style it's better to end with a newline than to begin with one. Commented 2 days ago
  • 1
    If the numbers for which you are searching are of different size, then I suggest that you use the data types int8_t, int16_t, int32_t and int64_t, instead of int or size_t. That way, it is clearer what size you expect the integers to be. Note that using these data types requires #include <stdint.h>. Commented 2 days ago
  • 1
    fseek(file, 0, SEEK_END); size_t Size = ftell(file); is bug-prone and not portable. See How do you determine the size of a file in C? Commented 2 days ago

1 Answer 1

3

The condition if (buffer==charTarget) will always be false, because both arrays will decay to pointers to the first elements of both arrays, and these pointer values will always be different, because the addresses of the arrays are different.

If you want to instead compare the content of the arrays, you can use the function memcmp.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! memcmp does provide a different "consistent" result. The result differs by a tiny amount, which I can deal later, but yeah, memcmp works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.