Address Book Data Base

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 15
Reputation: pulse0 is an unknown quantity at this point 
Solved Threads: 0
pulse0 pulse0 is offline Offline
Newbie Poster

Address Book Data Base

 
0
  #1
Apr 18th, 2007
I have a data base of 2000 people the following orientation:

[phone-number] [Name] [address] [zipcode]


all seperated by spaces in a text file.

I want a way to read this data into a struct:

struct person
{
long phone number[10];
string name[30];
string address[100];
int zip[5];
}

The address and the person's name (michael j. smith)have spaces in them.

What would be the best way to go about reading the whole file into this struct? or should I use something else instead of a struct?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Address Book Data Base

 
0
  #2
Apr 18th, 2007
a struct will be ok, but someone did not think very clearly when he/she created that database by making spaces the field separator when the field itself contains spaces. That will make it nearly impossible to read that file. Is there something else about the fields that distinguishes one field from another, such as are the fields all fixed-length, or surrounded by brackets as illustrated in the example you posted ?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Address Book Data Base

 
1
  #3
Apr 18th, 2007
You don't need a array of 5 ints to store a 5 digit integer. Change the struct to:
  1. struct person
  2. {
  3. long phone number;
  4. string name;
  5. string address;
  6. int zip;
  7. }

Abt reading from file and storing in struct. Post the code you write to do this and we'll help with any specific problem. See code snippets to get started with file reading and parsing.

Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 15
Reputation: pulse0 is an unknown quantity at this point 
Solved Threads: 0
pulse0 pulse0 is offline Offline
Newbie Poster

Re: Address Book Data Base

 
0
  #4
Apr 18th, 2007
No there are no brackets in that example I was just showing you how the file is. The width of phone number is 12 digits (with two -'s). The max width of name is 30. The max width of address is 100 and the zip is 5 digits long. I can try to place that in excel and format it manually and can insert a " | " character in between every field. Would that help?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Address Book Data Base

 
0
  #5
Apr 18th, 2007
when you say "max width" does that mean, for example, the name is always 30 characters even when the actual name is less than that? If that's true then you can modify the structure like this.
  1. struct person
  2. {
  3. char phone_number[10];
  4. char name[30];
  5. char address[100];
  6. char zip[5];
  7. };

Then read each line directly into one of those structure members
  1. person p;
  2.  
  3. ifstream in("file.txt",ios::binary);
  4. in.read(&p, sizeof(person));

Or, an alternative is to read the line and chop it up into fields
  1. struct person
  2. {
  3. std::string phone_number;
  4. std::string name;
  5. std::string address;
  6. std::string zip;
  7. };
  8.  
  9. person p;
  10. std::string line;
  11. ifstream in("file.txt");
  12.  
  13. getline(in,line);
  14. p.phone_number = line.substr(0,12);
  15. p.name = line.substr(13,43);
  16. p.address = line.substr(44,144);
  17. p.zip = line.substr(145);

Now just create a vector of person structs and put all the above (lines 13 thru 17) in a loop so that it reads the whole file. You may have to adjust the substrings a little because I may not have them right, but I think you get the idea.
Last edited by Ancient Dragon; Apr 18th, 2007 at 3:25 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 15
Reputation: pulse0 is an unknown quantity at this point 
Solved Threads: 0
pulse0 pulse0 is offline Offline
Newbie Poster

Re: Address Book Data Base

 
0
  #6
Apr 19th, 2007
Awesome!!! you are a life saver!!!
Thank you sooo much!!! I'll probabble have more questions about this 'cause I've just started this project!!


THis is such a great web-resource!!! I love this website already!
Last edited by pulse0; Apr 19th, 2007 at 11:21 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 15
Reputation: pulse0 is an unknown quantity at this point 
Solved Threads: 0
pulse0 pulse0 is offline Offline
Newbie Poster

Re: Address Book Data Base

 
0
  #7
Apr 23rd, 2007
few of the addresses are shorter than the specified length so when it reads to that max width it reads part of the next field in it also. I have delimited teh fields with a "|" character with no spaces in between.
every line is in the same syntax:
eg.
425-697-3142|john smith|123 main street #517|83210
313-111-2222|judy public|456 main street|90210


so any suggestions on reading the file in a way that every entry delemited by "|" is read into a different variable?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Address Book Data Base

 
0
  #8
Apr 23rd, 2007
>any suggestions on reading the file in a way that every entry delemited by "|" is read into a different variable?

Use getline to grab an entire line as already mentioned. Next, put it into a stringstream. Use getline again, but this time using '|' as a delimiter. Here's how the getline would look:
  1. getline(myStringstream, variable_to_extract, '|');
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Address Book Data Base

 
0
  #9
Apr 23rd, 2007
Also see this example and joe's explaination
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 15
Reputation: pulse0 is an unknown quantity at this point 
Solved Threads: 0
pulse0 pulse0 is offline Offline
Newbie Poster

Re: Address Book Data Base

 
0
  #10
Apr 23rd, 2007
ok for some strange reason my borland c++ 5.02 compiler is not letting me define string type variables? i'm including <string.h>

am I doing something wrong?

Here are the errors i'm getting.


Info :Compiling C:\DOCUMENTS AND SETTINGS\PULSE\DESKTOP\REGISTER\register.cpp
Error: register.cpp(7,6):Qualifier 'std' is not a class or namespace name
Error: register.cpp(7,8)tatement missing ;
Error: register.cpp(9,22):'is_open' is not a member of 'ifstream'
Error: register.cpp(14,13):Call to undefined function 'getline'
Error: register.cpp(14,25):Undefined symbol 'line'
Warn : register.cpp(14,25)tructure passed by value
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC