LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2009, 08:12 AM   #1
bhavyakm
LQ Newbie
 
Registered: Jan 2009
Posts: 7

Rep: Reputation: 0
Problem faced while using find_first_not_of


hi

I am programming in c++ and I am new to it.
Please find the attached code.
Please rename ExtractData.txt to ExtractData.cpp and
ContactDetails.txt to ContactDetails.

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.
Attached Files
File Type: txt ExtractData.txt (1.9 KB, 9 views)
File Type: txt ContactDetails.txt (369 Bytes, 8 views)
 
Old 11-20-2009, 08:20 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Hello Bhavyakm,

May I suggest that instead of attaching your files as "attachments", you instead post them directly within your thread, using [code] tags. This way, people can easily look directly at the code, without having to download these files and examine them in a viewer or text editor or whatever. Folks may be more inclined to have a look, if they do not need to mess around downloading files

Kind regards

Sasha
 
Old 11-22-2009, 10:10 PM   #3
bhavyakm
LQ Newbie
 
Registered: Jan 2009
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem faced during 'make install'.. Help me! Awg Semile Apong Linux - Software 2 03-05-2009 03:28 AM
Problem I faced in using rsyslog prakash.akumalla Linux - Software 0 06-26-2008 03:13 AM
Problem faced in installing Redhat Linux Ranjan_Muna Red Hat 8 04-28-2008 02:15 AM
problem faced regarding gcc amolbhor Programming 2 01-20-2007 03:43 AM
I have faced problem while configuring Kernal in RedHad 8.0 praful Linux - Newbie 1 02-08-2003 11:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration