Uncertain Behavior of String.find() in Loop

Hi Members,

This is a small utility program,which deletes the contents of a tag from an xml file.

In my case the tag is <call>,so ideally am trying to delete anything which is present inside <call> ~ </call>.

The deletion is based on a search criteria, if a particular function name is present in the xml file ,then only it should delete the complete tag (as mentioned above).

Approach:

(-) Read and pushed the function names to a vector (functionLists) .
(-) Read and pushed the xml file to a separate vector(vecOfStrs).
(-) Search the function name in the vector and delete from vector// AM Stuck
(-) Write back the contents from Vector to a new file.

As of now ,the program I have developed only deletes the function which is present at the end of the file.

Since I have put string.find() and vector.erase inside a for loop , I am expecting the contents to be deleted iteratively ,but in my case it only deletes 1 time.

The most baffling thing to me is the string.find() functionality ,it only fetches me the result once ,
e.g the function name which is present in the end of the file is only getting caught ,others are not ,if I change the order of function lists (functionList.txt),then also only for the last entry string.find() works and gets subsequently deleted.

cannot figure it out ,why it is happening ?

Any help in figuring it out will be of great help.

Attaching source code and other associated files.

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
75
76
77
78
# include <vector>
# include <fstream>
# include <iostream>
# include <algorithm>
using namespace std;

size_t readFunctionList(vector<string> &functionLists){
	
	size_t noOfFunc = 0;
	ifstream listOfFunctions("FunctionList.txt");
	if(listOfFunctions){
		string functionNames;
		while(getline(listOfFunctions,functionNames)){
			if(functionNames.size() >0 ){
				functionLists.push_back(functionNames);
				noOfFunc++;
			}
		}
	}
	listOfFunctions.close();
	return (noOfFunc);
}

void DeleteEntries(vector<string> &vecOfStrs,vector<string> &functionLists,size_t totalNumOfFunctions){

	size_t lineIndex,nextLines;
	string endToken = "</call>";

	ifstream sourceFile("Dummy_File.xml");
	if(sourceFile){
		string line;
		while(getline(sourceFile,line)){
			if(line.size() >0 ){
				vecOfStrs.push_back(line);
			}
		}
	}
	sourceFile.close();
	
	for(size_t maxFuncNum =0 ;maxFuncNum <totalNumOfFunctions ;maxFuncNum++) {
		
		for(lineIndex=0; lineIndex < vecOfStrs.size(); lineIndex++){
		
			if ( (vecOfStrs[lineIndex].find(functionLists[maxFuncNum]) ) != string::npos) {
				
				break;
			}
		}

		for (nextLines = lineIndex;nextLines<vecOfStrs.size();nextLines++){

			if ( (vecOfStrs[nextLines].find(endToken) ) != string::npos) {			
				break;
			}
		}
		vecOfStrs.erase(vecOfStrs.begin() + lineIndex, vecOfStrs.begin() + nextLines+1);
	}

}

void writeToFile (vector<string> &vecOfStrs){

	std::ofstream outFile("Dummy_File_new.xml");
	for (const auto &e : vecOfStrs) outFile << e << "\n";
}

int main(){

	vector<string> functionList ;
	vector<string> vecOfStr ;

	size_t totalNumOfFunction = readFunctionList(functionList);	
	DeleteEntries(vecOfStr,functionList,totalNumOfFunction);
	writeToFile(vecOfStr);

	return 0;
}


I will put the contents of both the files as unable to attach files at this point of time.


FunctionList.txt

1
2
3
4
PwrMgr_5
MainFunc_0
MainFunc_3
PwrMgr_0




Dummy_File.xml
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
      <Task FYI:type="Custsk" name="LoBuffer" active="false" >
        <Cust:annos>
          <Cust:anno Class="UII" value="DummyPath/Slow?type=custContainerVal"/>
        </Cust:annos>
        <CallSEK>
          <CallEntry FYI:type="CallSeq" name="LoBuff">
            <call FYI:type="FunctionCall" Function="PwrMgr_0">
              <Cust:annos>
                <Cust:anno Class="UII" value="DummyPath/Slow?type=custContainerVal"/>
              </Cust:annos>
            </call>
            <call FYI:type="FunctionCall" Function="PwrMgr_5">
              <Cust:annos>
                <Cust:anno Class="UII" value="DummyPath/Slow?type=custContainerVal"/>
              </Cust:annos>
            </call>
            <call FYI:type="FunctionCall" Function="MainFunc_0">
              <Cust:annos>
                <Cust:anno Class="UII" value="DummyPath//Time_MainFunc_0?type=custContainerVal"/>
              </Cust:annos>
              <execCond FYI:type="Counter" offset="0" Scaled="5"/>
            </call>
            <call FYI:type="FunctionCall" Function="MainFunc_3">
              <Cust:annos>
                <Cust:anno Class="UII" value="DummyPath/Time_MainFunc_3?type=custContainerVal"/>
              </Cust:annos>
              <execCond FYI:type="Counter" offset="0" Scaled="5"/>
            </call>
          </CallEntry>
        </CallSEK>
      </Task>




** Edit :

The correct file name is : FunctionList.txt, I had initially typed FunctionLists.txt
Last edited on
Learn how to use a debugger, set breakpoints and step through the code to observe what happens.


Breakpoint 4, readFunctionList (functionLists=std::vector of length 0, capacity 0) at foo.cpp:7
7	size_t readFunctionList(vector<string> &functionLists){
(gdb) n
9		size_t noOfFunc = 0;
(gdb) 
10		ifstream listOfFunctions("FunctionList.txt");
(gdb) 
11		if(listOfFunctions){
(gdb) 
20		listOfFunctions.close();
(gdb) 

Now this would have been a big "HUH!?" moment for you had you done this for yourself.

Note that in your text, you said the filename was FunctionLists.txt

With the right filename, my output file now reads

      <Task FYI:type="Custsk" name="LoBuffer" active="false" >
        <Cust:annos>
          <Cust:anno Class="UII" value="DummyPath/Slow?type=custContainerVal"/>
        </Cust:annos>
        <CallSEK>
          <CallEntry FYI:type="CallSeq" name="LoBuff">
          </CallEntry>
        </CallSEK>
      </Task>
@ Salem
Thanks for showing the code is working fine.
I tried debugging using print statements,it dint helped much.

I still dint get where the problem lies,it will be really nice ,if you point that ,moreover I will also try to debug using a debugger .

Regards
Nihar
Last edited on
Topic archived. No new replies allowed.