Easy String question

I am writing a function that finds a specific string in a character array (c style string). As far as I know something like this doesn't already exist in the string.h documentation (I don't want to compare the two... I want to find out where a specific string exists in a character array). For some reason my function isn't passing the correct argument.

Function should be pretty simple.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
int FindString(const char * buffer, const string str, const int bufferl) { //returns the placement in the buffer that the string we're looking for ends at

int i, j; 
j=0;

for(i=0; i < bufferl; ++i) {

	if(str.length() < j) 
		return i;

	else if(buffer[i] == str[j])
		++j;
		
		else j = 0; 
	}
	

		
	

}


int main() {

int i;
char * buffer;


ifstream file; 
int length;
file.open("2009_10_UEFA_Champions_League_group_stage.txt"); //I turn this text file into a character array to search through

file.seekg(0, ios::end); 
		length = file.tellg(); 
		file.seekg(0, ios::beg);
		
		//allocate memory
		
		buffer = new char[length];
		
		//read data as a block:
		file.read(buffer,length);
		file.close();
		
		int firstp;
		string firstteam, secondteam; //I'm looking for two team names
		
		
		firstp = FindString(buffer, "_v_", length); 
		
		for(i = firstp; buffer[i]!='"'; --i) // the team names are sandwhiched around"_v_"


		for(i; i<firstp; ++i)
			firstteam+=buffer[i];	

			
		for(i = firstp; buffer[i]!='"'; ++i)
			secondteam+=buffer[i];
			
		cout << endl << "The first team is" << firstteam;
		cout << endl << "The Second Team is" << secondteam;
			
		
		return 0;
		

		
		
		
		
		
		}



The code works but not as desired. I tried running this through GDB, breaking in findstring, and the problem is that FindString doesn't have the correct argument. I tried printing the variable str in FindString and instead it prints out something like


{static npos = 18446744073709551615,
_M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x7fffffffeb50 "( `"}}

no mention of "_v_" anywhere.

printing str.length() also yields a length of 4197536 . What the heck? Why isn't this function passing the correct argument?

Cheers,

-Trent
Last edited on
Edit:

Scrub that... it was no more than a hunch, and sticking:

cout << str << endl;

in at the start of your FindString function gives me a nice
_v_
output.
Last edited on
How strange. Why do we suppose that GDB says differently?
Here's my gdb output:


$1 = {static npos = 18446744073709551615, 
  _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x603028 "_v_"}}


str.length() gives me 3.
Last edited on
when I run it with the option "-g" it gives me that weird thing. but I get the correct parameter when I run it with -gstabs+
man wrote:
char *strstr(const char *haystack, const char *needle);
The strstr() function finds the first occurrence of the substring needle in the string haystack. The terminating '\0' characters are not compared.


The code works but not as desired
Be more specific
Very silly of me. I used a simpler test case and realized that I needed to change the return case for FindString from

if(str.length() < j)
return i;


to

if(str.length() <=j)
return j;
Topic archived. No new replies allowed.