|
-
February 3rd, 2008, 06:56 PM
#1
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;
}
-
February 3rd, 2008, 07:01 PM
#2
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...
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
February 3rd, 2008, 07:06 PM
#3
Re: adaption of code
thank you, this was an example given to me to show me how memcmp works. thank you again for help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|