Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,466 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,060 people online right now. Registration is fast and FREE... Join Now!




how to stop string termination

2 Pages V  1 2 >  
Reply to this topicStart new topic

how to stop string termination

tootypegs
4 Feb, 2008 - 11:31 AM
Post #1

D.I.C Head
**

Joined: 9 Oct, 2007
Posts: 177


My Contributions
Hi i want to search for the following string in a file.

CODE

static char match_criteria[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04 };



I have even tried to declare it as below but for some reason using memcmp() it wont pick it up yet i know its in my file as ive checked using a hex editor

CODE

unsigned char match_criteria [13] = "\x00\x00\x00\x00\x00\x04\x00\x03\x00\x00\x00\x04";



I presume the starting charcter of my search term is terminating my string so is there a way to search for blank characters so that i can match my string??

thanks
User is offlineProfile CardPM
+Quote Post

pertheusual
RE: How To Stop String Termination
4 Feb, 2008 - 02:15 PM
Post #2

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 233



Thanked: 4 times
My Contributions
Could you post the code for what you have so far?

It is hard to tell what could be going wrong without a little more information.
User is offlineProfile CardPM
+Quote Post

tootypegs
RE: How To Stop String Termination
4 Feb, 2008 - 02:21 PM
Post #3

D.I.C Head
**

Joined: 9 Oct, 2007
Posts: 177


My Contributions
This is the rest of the code, an example i was given to help me understand how memcmp() works. I can search and find other hex strings, for example 0xFF no problem, its just searching for my string as it contains blank spaces 0x00 thats when im having problems but its this sequence that i would like to match


CODE


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();
        
    }
    else if( memcmp( p_read_buffer, match_criteria, buf_len ) == 0 )
    {
        matched = true;
        pos = infile.tellg();
        pos -= buf_len;
        cout << "File: " << filename << " match search criteria at position: " << pos << endl;

    }
    }[b]
[/b]
User is offlineProfile CardPM
+Quote Post

pertheusual
RE: How To Stop String Termination
4 Feb, 2008 - 03:15 PM
Post #4

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 233



Thanked: 4 times
My Contributions
Okay, so you want to search through a file for that sequence of hexadecimal values?

To test I added:
CODE

ofstream outfile( filename.c_str(), ios::binary );
int i;
for(i = 0; i < buf_len; i++){
    outfile << match_criteria[i];
}
outfile.close();


to your program, right before it opened the file. It said it was able to find the hex values correctly.

The thing that I notice is that since it reads a whole buffer size at once, if the data was missaligned, then it would not find it.
For instance, if the file has 0x00 and right after that it has the sequence you are searching for, then it will miss it.

Perhaps that is what is causing the problem?
User is offlineProfile CardPM
+Quote Post

GWatt
RE: How To Stop String Termination
4 Feb, 2008 - 03:32 PM
Post #5

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,356



Thanked: 31 times
Dream Kudos: 500
My Contributions
Well, the zero value is used to terminate c strings. It's the signal to the system that it should stop at that memory address. You're not going to find a '\0' in the middle of a c string. You can get around that, but it's going to take much more work.

[edit]
and since you're using c++, why do you need to use c strings anyway? You have the string class.

This post has been edited by GWatt: 4 Feb, 2008 - 03:33 PM
User is offlineProfile CardPM
+Quote Post

tootypegs
RE: How To Stop String Termination
5 Feb, 2008 - 12:29 AM
Post #6

D.I.C Head
**

Joined: 9 Oct, 2007
Posts: 177


My Contributions
Yes i would like to search through my file for that string of hexadecimal values. Finding that particular sequence of 0x00'x is important to me so as u state its gunna take alot of work then i guess thats what i'm gunna need to do. If 0x00 terminates my string before its completely read could you point me in the direction of how i can get my program to look for the 0x00's without terminating?

I was advised to do it this way with searching for hex strings, if there is an eaasier way that will actually do what i would like i would like to know !!!!:) smile.gif smile.gif
User is offlineProfile CardPM
+Quote Post

pertheusual
RE: How To Stop String Termination
5 Feb, 2008 - 08:55 AM
Post #7

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 233



Thanked: 4 times
My Contributions
infile.read() should be able to read a binary file without stopping at 0x00's. That shouldn't cause any problems.

Have you checked to see if the data you are searching for is misaligned like I said in my previous post?

Right know you are going through the file in very large jumps, so if your data is misaligned, it would miss it.
For instance the sequence "1234"
would NOT be found in "30123476" because it would read "3012" and then "3476", neither of which match.

Try adding a seek at the beginning of each loop, and seek 1 byte farther each time.

So add
CODE

infile.seekg (position, ios::beg);

right before your read, and increment "position" each time around the loop.
User is offlineProfile CardPM
+Quote Post

tootypegs
RE: How To Stop String Termination
5 Feb, 2008 - 09:54 AM
Post #8

D.I.C Head
**

Joined: 9 Oct, 2007
Posts: 177


My Contributions
right ok then, i think you could be right by by stating i have misaligned data in my file as i have run a few tests using different criteria and it did fail to pick it up.

I have add the line hat you suggested to my file

CODE

         infile.seekg (pos, ios::beg);        // Your line as suggested
        infile.read( p_read_buffer, buf_len );
    if( infile.eof() || infile.fail() || infile.bad() )
    {
        infile.close();
        
    }
    else if( memcmp( p_read_buffer, match_criteria, buf_len ) == 0 )
    {
  
matched = true;

        pos = infile.tellg();
        pos -= buf_len;
        cout << "File: " << filename << " match search criteria at position: " << pos << endl;

    }
    }



however I am confused as to prgress. I'm not understanding why my prog wont pick up the data because of misallignment. I understand what it is but i thought that my prog would automatically check that the search criteria would fully match the parts of my file? would it be possible to explain to me how what you suggest will fix my problem? - i am saying what you suggest would be wrong - infact i HOPE it fixes my prob smile.gifsmile.gif, only that im not totally understanding


...and thanks for patience and help!!
User is offlineProfile CardPM
+Quote Post

pertheusual
RE: How To Stop String Termination
5 Feb, 2008 - 11:52 AM
Post #9

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 233



Thanked: 4 times
My Contributions
The variable that you are using for position is not correct. You can't use the same position as the one that you are getting from seek, you just need to:

pseudocode

num = 0;
while(not end){
seek to num;
read into buffer
increase num by one
}

The position that you are using wouldn't make the flow of reading correct.

CODE

    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 );
    int position = 0;

    while( infile.is_open() && infile.good() && !infile.eof() && !infile.fail() && !infile.bad() )
    {
        infile.seekg(position, ins::beg);
        infile.read( p_read_buffer, buf_len );
    if( infile.eof() || infile.fail() || infile.bad() )
    {
        infile.close();
        
    }
    else if( memcmp( p_read_buffer, match_criteria, buf_len ) == 0 )
    {
        matched = true;
        pos = infile.tellg();
        pos -= buf_len;
        cout << "File: " << filename << " match search criteria at position: " << pos << endl;

    }

    position++;
    }

User is offlineProfile CardPM
+Quote Post

tootypegs
RE: How To Stop String Termination
5 Feb, 2008 - 02:11 PM
Post #10

D.I.C Head
**

Joined: 9 Oct, 2007
Posts: 177


My Contributions
so by doing it this way i am stopping the chances of missing the search criteria. Is it sort of starting at every byte and trying to match the string, if it does not match it moves to next byte and starts again? I hope im starting to grasp the idea. When i implimented the code it seems to have stopped the variable 'pos' and 'matched' from working saying they have a value that is never used? is this because it doesnt make it the the 'else if' part of the if statement?

thanks
User is offlineProfile CardPM
+Quote Post

pertheusual
RE: How To Stop String Termination
5 Feb, 2008 - 09:21 PM
Post #11

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 233



Thanked: 4 times
My Contributions
If it is saying that those values are not used, then there must be something that has changed somewhere in the code then. Are you certain that you copied that code exactly as it is above?

Your description of the search is correct.
This will take "2345891456", and search for "89"
So it would go "23"? No
"34"? No
"45" No
"58" No
"89" yes



User is offlineProfile CardPM
+Quote Post

tootypegs
RE: How To Stop String Termination
6 Feb, 2008 - 12:26 AM
Post #12

D.I.C Head
**

Joined: 9 Oct, 2007
Posts: 177


My Contributions
yes i tried your code snippet. It compiled only stating 'pos' and 'matched' were assigned values that are never used. When it is run it doesnt print anything to the screen.

however i did get to this line
CODE

infile.seekg(position, ins::beg);



and changed 'ins' to 'ios'. Could this becausing the problem as trying to compile it with 'ins' kept giving my an error.

This post has been edited by tootypegs: 6 Feb, 2008 - 12:28 AM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 1/7/09 02:11PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month