Thread: Searching a binary file

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    17

    Searching a binary file

    I am trying to write a function which searches a binary file for the occurance of a particular string of characters. I've made some progress but have got a little stuck. Here's what I have come up with so far
    Code:
    void searchRandomObjectForName(string fname)
        
    {
        	int index = 0;
        	string search, name;
        	cout << "Enter a string to search the file for: " << flush;
             cin >> search;
        	ifstream is(fname.c_str(), ios::binary); // open file to read
    
             // while we can read from the file
             while(is.read(reinterpret_cast<char *>(&name), sizeof(int)))
             {
          	    // search here, print out string and position in the file
                // or print "string not found message."
             }
              is.close(); // close file when done
        	cout << endl;
    }
    Can anybody help out?
    Last edited by rhysmeister; 04-18-2004 at 01:56 PM. Reason: code tages / formatting

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is strstr().

    Kuphryn

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The file is binary, so I guess it'd be safe to assume that strings aren't the best thing to store data in.

    Here's one way of doing what you want
    Code:
     #include <iostream>
     #include <fstream>
     
     int main()
     {  
       char  LookForArray[] = "\x11\x12\x13";
       int   Len = sizeof(LookForArray) - 1;
       int   LookForByte = 0;
       int   c;
       std::ifstream is("myfile.bin", std::ios::binary);
       
       while (LookForByte < Len && (c = is.get()) != EOF)
       {
     	if (c == LookForArray[LookForByte])
     	  LookForByte++;
     	else LookForByte = 0;
       }
       
       if (LookForByte == Len)
       {
     	std::cout <<"item was found" <<std::endl;
       }
       else
       {
     	std::cout <<"item was not found" <<std::endl;
       }
       
       is.close();
     }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Another solution is memchr().

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM