944,108 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4357
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 18th, 2007
0

Address Book Data Base

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pulse0 is offline Offline
15 posts
since Apr 2007
Apr 18th, 2007
0

Re: Address Book Data Base

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 ?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Apr 18th, 2007
1

Re: Address Book Data Base

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.

Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Apr 18th, 2007
0

Re: Address Book Data Base

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pulse0 is offline Offline
15 posts
since Apr 2007
Apr 18th, 2007
0

Re: Address Book Data Base

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Apr 19th, 2007
0

Re: Address Book Data Base

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pulse0 is offline Offline
15 posts
since Apr 2007
Apr 23rd, 2007
0

Re: Address Book Data Base

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pulse0 is offline Offline
15 posts
since Apr 2007
Apr 23rd, 2007
0

Re: Address Book Data Base

>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:
C++ Syntax (Toggle Plain Text)
  1. getline(myStringstream, variable_to_extract, '|');
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 23rd, 2007
0

Re: Address Book Data Base

Also see this example and joe's explaination
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Apr 23rd, 2007
0

Re: Address Book Data Base

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pulse0 is offline Offline
15 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Simple script parser - how to ?
Next Thread in C++ Forum Timeline: passing strings as functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC