Join 149,466 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,060 people online right now. Registration is fast and FREE... Join Now!
I have even tried to declare it as below but for some reason using memcmp() it wont pick it up yet i know its in my file as ive checked using a hex editor
I presume the starting charcter of my search term is terminating my string so is there a way to search for blank characters so that i can match my string??
This is the rest of the code, an example i was given to help me understand how memcmp() works. I can search and find other hex strings, for example 0xFF no problem, its just searching for my string as it contains blank spaces 0x00 thats when im having problems but its this sequence that i would like to match
Okay, so you want to search through a file for that sequence of hexadecimal values?
To test I added:
CODE
ofstream outfile( filename.c_str(), ios::binary ); int i; for(i = 0; i < buf_len; i++){ outfile << match_criteria[i]; } outfile.close();
to your program, right before it opened the file. It said it was able to find the hex values correctly.
The thing that I notice is that since it reads a whole buffer size at once, if the data was missaligned, then it would not find it. For instance, if the file has 0x00 and right after that it has the sequence you are searching for, then it will miss it.
Well, the zero value is used to terminate c strings. It's the signal to the system that it should stop at that memory address. You're not going to find a '\0' in the middle of a c string. You can get around that, but it's going to take much more work.
[edit] and since you're using c++, why do you need to use c strings anyway? You have the string class.
This post has been edited by GWatt: 4 Feb, 2008 - 03:33 PM
Yes i would like to search through my file for that string of hexadecimal values. Finding that particular sequence of 0x00'x is important to me so as u state its gunna take alot of work then i guess thats what i'm gunna need to do. If 0x00 terminates my string before its completely read could you point me in the direction of how i can get my program to look for the 0x00's without terminating?
I was advised to do it this way with searching for hex strings, if there is an eaasier way that will actually do what i would like i would like to know !!!!:)
infile.read() should be able to read a binary file without stopping at 0x00's. That shouldn't cause any problems.
Have you checked to see if the data you are searching for is misaligned like I said in my previous post?
Right know you are going through the file in very large jumps, so if your data is misaligned, it would miss it. For instance the sequence "1234" would NOT be found in "30123476" because it would read "3012" and then "3476", neither of which match.
Try adding a seek at the beginning of each loop, and seek 1 byte farther each time.
So add
CODE
infile.seekg (position, ios::beg);
right before your read, and increment "position" each time around the loop.
right ok then, i think you could be right by by stating i have misaligned data in my file as i have run a few tests using different criteria and it did fail to pick it up.
I have add the line hat you suggested to my file
CODE
infile.seekg (pos, ios::beg); // Your line as suggested infile.read( p_read_buffer, buf_len ); if( infile.eof() || infile.fail() || infile.bad() ) { infile.close();
however I am confused as to prgress. I'm not understanding why my prog wont pick up the data because of misallignment. I understand what it is but i thought that my prog would automatically check that the search criteria would fully match the parts of my file? would it be possible to explain to me how what you suggest will fix my problem? - i am saying what you suggest would be wrong - infact i HOPE it fixes my prob , only that im not totally understanding
The variable that you are using for position is not correct. You can't use the same position as the one that you are getting from seek, you just need to:
pseudocode
num = 0; while(not end){ seek to num; read into buffer increase num by one }
The position that you are using wouldn't make the flow of reading correct.
so by doing it this way i am stopping the chances of missing the search criteria. Is it sort of starting at every byte and trying to match the string, if it does not match it moves to next byte and starts again? I hope im starting to grasp the idea. When i implimented the code it seems to have stopped the variable 'pos' and 'matched' from working saying they have a value that is never used? is this because it doesnt make it the the 'else if' part of the if statement?
If it is saying that those values are not used, then there must be something that has changed somewhere in the code then. Are you certain that you copied that code exactly as it is above?
Your description of the search is correct. This will take "2345891456", and search for "89" So it would go "23"? No "34"? No "45" No "58" No "89" yes
yes i tried your code snippet. It compiled only stating 'pos' and 'matched' were assigned values that are never used. When it is run it doesnt print anything to the screen.
however i did get to this line
CODE
infile.seekg(position, ins::beg);
and changed 'ins' to 'ios'. Could this becausing the problem as trying to compile it with 'ins' kept giving my an error.
This post has been edited by tootypegs: 6 Feb, 2008 - 12:28 AM