-
adaption of code
Hi i have the following code that looks for a string of hex in a file then tells me if the file contains the string and tells me the number of byte the string begins at. However i know that my string appears in the file more than once and i would like my code to find every instance of the string and report the byte number that every string starts at. Can anyone help me to adapt this or point me in the right direction?
thank you
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
static char match_criteria[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04 };
size_t buf_len = (sizeof( match_criteria )/sizeof( char ));
char* p_read_buffer = new char[buf_len];
int pos = 0;
bool matched = false;
cout << "Enter the name of the file you wish to open: ";
string filename;
getline( cin, filename );
ifstream infile( filename.c_str(), ios::binary );
while( infile.is_open() && infile.good() && !infile.eof() && !infile.fail() && !infile.bad() )
{
infile.read( p_read_buffer, buf_len );
if( infile.eof() || infile.fail() || infile.bad() )
{
infile.close();
break;
}
else if( memcmp( p_read_buffer, match_criteria, buf_len ) == 0 )
{
matched = true;
pos = infile.tellg();
pos -= buf_len;
break;
}
}
if( matched )
{
cout << "File: " << filename << " match search criteria at position: " << pos << endl;
}
else
{
cout << "Match criteria not found in: " << filename << endl;
}
delete p_read_buffer;
return 0;
}
-
Re: adaption of code
Simply get rid of the break, and move the output into the loop.....
Don't see why you would have a problem with this if you wrote the original code... :confused: :confused: :confused:
-
Re: adaption of code
thank you, this was an example given to me to show me how memcmp works. thank you again for help