I'm trying to search a binary file for some hex values and then replace them, using C++ CLR. Reading and writing the file works, but I just haven't found a way to search and replace successfully. Here is the relevant code I've created:

array<Byte>^ fileContents = File::ReadAllBytes(item);
textBox2->Text = item;

Object^ search = '\x6E' + '\x73' + '\x61' + '\x76' + '\x00' + '\x00' + '\x00' + '\x01';
int index = Array::BinarySearch(search, fileContents);

String^ filename = item->Replace(L".mov", L"_new.mov");
textBox2->Text = filename;

File::WriteAllBytes(filename, fileContents);

>but I just haven't found a way to search and replace successfully
Create a new file, write the changed contents to it, and delete the old file.

Please read my initial post, your reply wasn't helpful at all.

Is Array::BinarySearch() something you coded? And if it is, then please post it. Binary searches only work on sorted data. So if the data isn't sorted than the program must do a linear search.

I loaded a file into the program in binary mode into an array of bytes. It hasn't been sorted because I just want to modify some hex values in it. I don't know how to search the array for these values and which function to use. (Array::BinarySearch() was just a guess, but I don't know how to make it work).

I would start at the beginning and use a pointer to find the first type of the sequence, then memcmp() to determine if it is the first byte of the entire sequence. Something like this (but may not really work in managed programs. I don't write managed code programs so don't be surprised if this doesn't compile correctly.).

unsigned char* ptr = fileContents;
Object^ search = '\x6E' + '\x73' + '\x61' + '\x76' + '\x00' + '\x00' + '\x00' + '\x01';
unsigned char* searchPtr = search;
for(int i = 0; i < fileContents.Length; i++, ptr++)
    if( *ptr == *searchPtr )
        if( memcmp(ptr, searchPtr) == 0)
            // found it!