• 0

[C++] Searching a char for a string.


Question

I've been trying to find a way to do this, but after hours of playing around i can't get it working..

I have a file that i want to read.. it's 1.5mb.. and contains data and strings.

So i open the file with an ifstream in binary mode, create a buffer, dump all data to it, then close the file. Now my buffer contains the file for searching..

however.. now i'm stuck in terms of how to search for a specific keyword.

this is what i have so far.

//Open our file
  ifstream myFile(ourfile, ifstream::binary);

  //get length of the file.
  unsigned int length;
  myFile.seekg(0, ios::end);
  length = myFile.tellg();
  myFile.seekg(0, ios::beg);

  //create a buffer to hold the entire file
  char *buffer = new char [length];

  //read the file into our buffer
  myFile.read(buffer,length);

  //close the file as we don't need it anymore.
  myFile.close();

I want to find a specific keyword's position in the buffer.

hopefully without looping every character.. like this..

(example)

"hello_cruel_world"

i don't want to have to search like this...

hello -> ello_ -> llo_c -> lo_cr -> o_cru -> _crue -> cruel

cruel == cruel.

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

I believe that strstr will do a linear search, and I believe if the file is totally random data then you will be forced to search it linear.

Your alternative choice is to but your data in an appropriate structure (sort them). Then later when you read the file make use some search algorithm on them.

Link to comment
Share on other sites

  • 0

*EDIT* correction.. it works with char *buffer.. but stops at first NULL found.

i tried strstr() before, and it doesn't want to work with char *buffer... unless i'm missing something.

 char test[] = "This is a test example";

  char *pos;
  pos = strstr(buffer, "keyword");
  printf("0x%08X\n", pos);

with test it works.. with buffer it doesn't.

Edited by Bi0haZarD
Link to comment
Share on other sites

  • 0
i tried strstr() before, and it doesn't want to work with char *buffer... unless i'm missing something.

 char test[] = "This is a test example";

  char *pos;
  pos = strstr(buffer, "keyword");
  printf("0x%08X\n", pos);

with test it works.. with buffer it doesn't.

There is few things I don't understand could you make them clear

Why are you opening the file as binary ? is it a binary file or text file?

why should you read the all of the file in memory? can u read line by line or something

to trace your issue I need a sample file and code add me on msn

redfox2200 @ hotmail.com

Link to comment
Share on other sites

  • 0

oops forgot to say what happens when it runs lol.. It returns null (couldn't find it).

Correction on my part.. char *buffer works.. (tested with a small file with a random sentence.)

however when trying on a binary file it stops at the first sign of NULL(00).

to test this, i took a big chunk out of the file i want to search, it returned null (as it hit a null before my keyword was found).

then using a hex editor; i replaced all nulls to nothing, and it found the keyword. (doesn't find a null so it keeps looking until EOF.)

so to clarify.. an example... say i had..

48 65 6C 6C 6F 20 57 6F 72 6C 64 2E (Hello{space}World.)

48 65 6C 6C 6F 00 57 6F 72 6C 64 2E (Hello{NULL}World.)

The first one would find "World" as a null is never hit.

The second one would fail.. as null is hit before it finds "World".

and i'm opening the file as binary because it contains a lot of random data.. it's not just a bunch of text/strings in a file.

Edited by Bi0haZarD
Link to comment
Share on other sites

  • 0
oops forgot to say what happens when it runs lol.. It returns null (couldn't find it).

Correction on my part.. char *buffer works.. (tested with a small file with a random sentence.)

however when trying on a binary file it stops at the first sign of NULL(00).

to test this, i took a big chunk out of the file i want to search, it returned null (as it hit a null before my keyword was found).

then using a hex editor; i replaced all nulls to nothing, and it found the keyword. (doesn't find a null so it keeps looking until EOF.)

so to clarify.. an example... say i had..

48 65 6C 6C 6F 20 57 6F 72 6C 64 2E (Hello{space}World.)

48 65 6C 6C 6F 00 57 6F 72 6C 64 2E (Hello{NULL}World.)

The first one would find "World" as a null is never hit.

The second one would fail.. as null is hit before it finds "World".

and i'm opening the file as binary because it contains a lot of random data.. it's not just a bunch of text/strings in a file.

Well then that means strstr is working as intended. You may just have to create your own function to do the search.

Link to comment
Share on other sites

  • 0
Well then that means strstr is working as intended. You may just have to create your own function to do the search.

figured as much, so made a small linear binary search function to do it.. finds the offset within a second so works for me.

thanks for the help guys.

Link to comment
Share on other sites

  • 0

Although I'm a bit late, I'd suggest you implement the Turbo Boyer-Moore algorithm for something like this as it'd be much more efficient than a plain linear search.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.