search hex values in a file

Aug 24, 2010 at 6:19pm
I'm trying to look for a method or function code to look/search for a specific hex value in a file

I'm want to look for this value in a file

0x67452301

Like if I open a hex editor, it shows "67 45 23 01"

I just want to check if those values exist in a file or not, that all...

Last edited on Aug 24, 2010 at 6:20pm
Aug 24, 2010 at 6:53pm
If the file isn't large, you could read it all into a char array (using read()) and the use http://www.cplusplus.com/reference/algorithm/search/ to find what you need.
Another way would be to read one byte. Then compare it to 67. If there's no match, repeat. If there is a match, read another byte and compare it to 45, and so on..
Aug 24, 2010 at 7:30pm
Use Boost regex, passing in a pair of istream_iterators. I'd suggest doing that with std::search, but it requires forward iterators, not input iterators. Boost regex supports searching with input iterators. Though the documentation is a bit ambiguous, I know it can be made to work. I've got code that reads and parses data from std::cin using streambuf_iterators.

http://www.boost.org/doc/libs/release/libs/regex/index.html
Aug 24, 2010 at 8:49pm
Depending on what you want to do with the result you might want to consider the benefits that stream iterators have to offer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <fstream>
#include <iterator>
#include <iostream>
#include <algorithm>

int main()
{
	std::ifstream ifs("input.txt");

	// begin = start of file
	// end = end of file
	std::istream_iterator<char> begin(ifs), end;

	// string to match
	std::string str = "\x67\x45\x23\x01";

	// search file for string
	if(std::search(begin, end, str.begin(), str.end()) != end)
	{
		std::cout << "found" << std::endl;
	}
}
Last edited on Aug 24, 2010 at 8:51pm
Aug 25, 2010 at 2:14am
Galik, According to the docs, std::search requires a ForwardIterator, and std::istream_iterator models an InputIterator.

http://www.cplusplus.com/reference/algorithm/search/
http://www.cplusplus.com/reference/std/iterator/istream_iterator/

It compiles but doesn't work on at least one platform I have access to (Solaris + Sun Studio).

That's because it's std::search implementation calls std::distance, which consumes all of the input before actually searching the text.
Aug 25, 2010 at 3:03am
That's interesting. You're right the iterator types don't match. That's a shame, it works on my Linux implementation. After the search is successful the input stream is only consumed up to (and including) the matched portion. But that evidently can't be relied upon...
Aug 25, 2010 at 6:04pm
Thank you Galik, that wat I was looking for, the iterator type was the key for me.

Thanks all you guys for helping for the inputs and advice as well. Worked out great.
Last edited on Aug 25, 2010 at 6:05pm
Aug 25, 2010 at 6:22pm
I kinda found a problem, if the string str = "\x0G";

It still displayed found but G isn't part of the hex.
Last edited on Aug 25, 2010 at 6:22pm
Aug 25, 2010 at 6:40pm
A couple of things. First, my method has a fault as PanGalactic pointed out. It works on some systems but not others, so its not a good idea to use it. Second "\x0G" is not a HEX value. You shouldn't enter something like that into a string literal. I tried it on my system and it did not complain but it is equivalent to using an empty string "". An empty string is always found.
Last edited on Aug 25, 2010 at 6:41pm
Aug 25, 2010 at 6:46pm
O ok, thanks the explanation. now got the better understanding of it.
Aug 30, 2010 at 6:50pm
I'm having this crazy situation...

I'm using cygwin to compile and run the file, and it works perfectly.

Then I test it again, using QT creator, it builds fine, but when I run the program. It does not seem to work. I check to see if the program actually open the files, and it does, but it does not do the search.

So I'm guess this where PanGalactic pointed out applies?
Last edited on Aug 30, 2010 at 7:07pm
Aug 30, 2010 at 7:03pm
It could well be. There is no reason that my method should work the same on two different compiler/library implementations.
Topic archived. No new replies allowed.