Hi,
Thank you for the suggestion.
Please find the code where i am facing problem using find_first_not_of.
Code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
FILE *fp;
fp=fopen("ContactDetails","r");
if (fp == NULL)
{
printf("Failed to open File\n");
}
char * line = NULL;
size_t len = 0;
ssize_t read;
string st,st1;
string flag = "false";
string firstName="",lastName="",fullName="";
while(!feof(fp))
{
int found=0;
if ((read = getline(&line, &len, fp))!=-1)
{
st1 = string(line);
printf("\n************DATA READ FROM FILE=%s\n",line);
found = st1.find("cn:");
if(found==0)
{
string st2=st1.substr(st1.find_first_not_of("cn: "),st1.length()-st1.find_first_not_of("cn: ")-1);
}
found=st1.find("givenName");
if(found==0)
{
printf("found=%d ,st=%s\n",found,st1.c_str());
int found1 = st1.find_first_not_of("givenName: ");
int found2 = (st1.length()-st1.find_first_not_of("givenName: ")-1);
printf("found1=%d,found2=%d\n",found1,found2);
string st2=st1.substr(st1.find_first_not_of("givenName: "),(st1.length()-st1.find_first_not_of("givenName: ")-1));
firstName = st2 ;
printf("\nFirstName=%s =%s\n",firstName.c_str(),st2.c_str());
}
found=st1.find("sn");
if(found==0)
{
printf("found=%d ,st=%s\n",found,st1.c_str());
int found1 = st1.find_first_not_of("sn: ");
int found2 = (st1.length()-st1.find_first_not_of("sn: ")-1);
printf("found1=%d,found2=%d\n",found1,found2);
string st2=st1.substr(st1.find_first_not_of("sn: "),st1.length()-st1.find_first_not_of("sn: ")-1);
lastName = st2 ;
printf("\nLastName=%s =%s\n",lastName.c_str(),st2.c_str());
}
found=st1.find("fullName");
if(found==0)
{
string st2=st1.substr(st1.find_first_not_of("fullName: "),(st1.length()-st1.find_first_not_of("fullName: ")-1));
fullName = st2 ;
printf("\nfullName=%s =%s\n",fullName.c_str(),st2.c_str());
}
}
}
fclose(fp);
}
File from which data needs to be extracted.
(ContactDetails)
cn: 001
fullName: ABC XYZ
givenName: ABC
mail: sdsd
cn: 002
fullName: xyz qwerty
givenName: qwerty
cn: 003
fullName: john dias
givenName: john
sn: dias
cn: 004
givenName: er4
sn: da
cn: 005
givenName: abc5
sn: da
cn: 006
givenName: av6
sn: da
cn: 007
givenName: asdf7
sn: da
cn: 008
givenName: lkj8
sn: da
cn: 009
givenName: asdf9
sn: da
Mainly the code extracts data from a file(ContactDetails)based on finding keyword.
Problem faced:
In code everytime i look for keyword say "givenName:: " using find and then extract the givenName information using substr and find_first_not_of.
st.find_first_not_of("givenName:: ") will give the result to be 11,12,13 which is not correct and the value should be 11.
I think there is bug in find_first_not_of.
Please help me and thanks in advance.