search and extract

Hi

if you look at the data below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
dc.w SME_71Usz_C-SME_71Usz, SME_71Usz_35-SME_71Usz	
		
SME_71Usz_C:	dc.b 8	
		
dc.b $E0, $F, 0, 0, $F8	
		
dc.b $E8, 1, 0, $10, $F0	
		
dc.b 0, 8, 0, $12, 0	
		
dc.b 8, $C, 0, $15, $F8	
		
dc.b $10, $C, 0, $19, 0	
		
dc.b $10, 0, 0, $1D, $20	
		
dc.b $18, $D, 0, $1E, $10	
		
dc.b $20, 4, 0, $26, 0	


what can I use to find the "dc.b" string then extract the 5 bytes after it. I probably could use sscanf to extract the bytes eg.

sscanf (txt,"dc.b $%X, $%X, %X, %X, $%X",&x,&size,&flip,&t,&y);

Last edited on
bump
closed account (zb0S216C)
What you could do is extract each line into a std::vector, where each element is of type std::string. The beauty of std::strings is that they allow easy manipulation of their contents.

So, here's how I would lay it out:

1) Extract an entire line into a std::string with std::getline( ).
2) Add the string to a vector.
3) When all extraction operations are done, search for "dc.b".
4) Once found (if found), do such and such to that line.

Wazzak
Last edited on
thanks Framework

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;


int main (){
	vector<string> data;
	string txt = "";
	fstream inFile;
	inFile.open ("source.txt");
	while(!inFile.eof())
		{	getline(inFile,txt);
			data.push_back(txt);
		}
	return 0;
}


would do I use to search for "dc.b" in the vector
can you give an example
Last edited on
closed account (zb0S216C)
Perhaps something like this:

1
2
3
4
5
6
7
for( unsigned int I( 0 ); I < data.size( ); I++ )
{
    if( data.at( I ).compare( "dc.b" ) == 0 )
    {
        // Match.
    }
}

This code is easy to understand. All it does is call the std::string::compare( ) method for each element within the data vector.

Wazzak
Last edited on
The problem with this code is that the elements within the data vector are stored like this

1
2
3
"	       dc.b $E0, $F, 0, 0, $F8	"	
"              dc.b $E8, 1, 0, $10, $F0	"
"	       dc.b 0, 8, 0, $12, 0	"


so the .compare becomes redundant in this case, due to the whole element being compared to "dc.b" and not matching. However I did something like this


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdio.h>
using namespace std;


int main (){
	vector<string> data;
	string txt = "";
	fstream inFile;
	inFile.open ("source.txt");
	while(!inFile.eof())
		{	getline(inFile,txt);
			string key = "dc.b";
			int found = -0x1;
			found =  txt.rfind(key);
			if (found != -0x1){
				data.push_back(txt);}
		}
	return 0;
}


This basically puts anything with "dc.b" in it into the vector and discarding any redundant data. Now my problem is what do i use to extract the 5 bytes from each element.
closed account (zb0S216C)
Sorry about my code, it was a late-night post ^_^ What you could do is tokenize each string and parse each token accordingly. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::string String( "dc.b $E0, $F, 0, 0, $F8" );

char *Token( nullptr );

Token = strtok( ( char * )String.c_str( ), " ," );

while( Token != nullptr )
{
    Token = strtok( nullptr, " ," );

    if( Token == nullptr )
        break;

    // Show each token.
    std::cout << Token << std::endl;
}

Of course, this pulls in C functionality but it works.

Wazzak
Last edited on
Topic archived. No new replies allowed.