CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2007
    Posts
    52

    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;
    }

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  3. #3
    Join Date
    Sep 2007
    Posts
    52

    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
  •  





Click Here to Expand Forum to Full Width

Featured