Hello, Relatively new to C++ and working on a program that parses data input from a file. I have some values stored in vector<string> arrays and I have to compare different columns of the input for various reasons. The problem I'm having:
I do a string compare on 2 strings that as far as I can see, are the same. I've checked the string size() and its teh same. I've tried to use substrings to make sure they are the same size and it still doesnt recoginze that they are the same.
Here is what I'm doing, code posted below.
Line of input is parsed into strings. Say one string is this:LISTA,ENDA
I use a string explode function(in the code below) that seperates the string into a vector array using the comma as a seperator.
I then scan part of a struct that also holds strings and compare them to see if they are the same. Looking at the strings, printing them to screen etc I see no difference in the strings but the .compare method does not recoginize they are the same string. I'm thinking there may be a problem with the stringexplode function but I cant figure out what. Any help would be appreciated. I've posted partial code because my entire code is to long. If you have any insights on the stringexplode function let me know. I'm not averse to using another method to explode the string if thats the problem.
One more thing, the first element of the array created by stringexplode is compared fine, it finds it match, its just the second element ENDA that is n ot being found which is why I think its teh explode function but I cant figure out what is wrong.
Code
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
|
struct ext_sym_table{
int ctl_sec;
string ctl_sec_name;
string symbol;
int loc;
int ctl_sec_length;
char type;
}eSymTab;
//create extsymtab for current file
eSymTab.ctl_sec = ctl_secctr;
eSymTab.ctl_sec_name = headerName;
eSymTab.ctl_sec_length = locctr - (input[0].loc);
eSymTab.loc = input[0].loc;
estab.push_back(eSymTab);
for(int i=0;i<extdefs.size();i++){
string exdef = extdefs[i];
cout<<"Exdef: "<<exdef<<endl;
for(int j=0;j<input.size();j++){
string lab = symtab[j].symbol;
cout<<"Label: "<< lab<<endl;
if(extdefs[i].compare(symtab[j].symbol)==0){
//.compare((symtab[j].symbol).substr(0,exdef.size()))==0){
cout<<"YESH"<<endl;
}else{
cout<<"NOOOOO"<<endl;
cout<<"e: "<<exdef[0]<<" "<<exdef[1]<<" "<<exdef[2]<<" "<<exdef[3]<<" "<<exdef[4]<< exdef[5]<<endl;
cout<<"l: "<<lab[0]<<" "<<lab[1]<<" "<<lab[2]<<" "<<lab[3]<<" "<<lab[4]<< lab[5]<<endl;
cout<<"error: "<<exdef.compare(lab.substr(0,exdef.size()))<<endl;
char *one = new char[exdef.size()+1];
one[exdef.size()]=0;
memcpy(one,exdef.c_str(),exdef.size());
char *two = new char[lab.size()+1];
two[lab.size()]=0;
memcpy(two,lab.c_str(),lab.size());
cout<<"Error2 "<<strcmp(one,two)<<endl;
}
}
}
void StringExplode(string str, string separator, vector<string>* results){
int found;
found = str.find_first_of(separator);
while( found!=string::npos){
if(found>0){
results->push_back(str.substr(0,found));
}
str = str.substr(found+1);
found = str.find_first_of(separator);
}
if(str.length()>0){
results->push_back(str);
}
}
|
Input file
1 2 3 4 5 6 7 8 9 10 11 12
|
0000 PROGA STARTX 0XXXXXXXXXXXXXXXXXXXXXA 0000000A
EXTDEF LISTA,ENDA
EXTREF LISTB,ENDB,LISTC,ENDC
0006 CLOOP +JSUB RDREC 4B101036
0020 REF1 LDA LISTA 03201D
0023 REF2 +LDT LISTB+4 77100004
0040 LISTA EQU *
0054 ENDA EQU *
00A7 REF3 LDX #ENDA-LISTA 050014
1036 RDREC CLEAR X B410
END REF1
|