| | |
Address Book Data Base
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 15
Reputation:
Solved Threads: 0
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?
[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?
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.
You don't need a array of 5 ints to store a 5 digit integer. Change the struct to:
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.
c Syntax (Toggle Plain Text)
struct person { long phone number; string name; string address; int zip; }
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.
•
•
Join Date: Apr 2007
Posts: 15
Reputation:
Solved Threads: 0
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?
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.
Then read each line directly into one of those structure members
Or, an alternative is to read the line and chop it up into fields
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.
c Syntax (Toggle Plain Text)
struct person { char phone_number[10]; char name[30]; char address[100]; char zip[5]; };
Then read each line directly into one of those structure members
c Syntax (Toggle Plain Text)
person p; ifstream in("file.txt",ios::binary); in.read(&p, sizeof(person));
Or, an alternative is to read the line and chop it up into fields
c Syntax (Toggle Plain Text)
struct person { std::string phone_number; std::string name; std::string address; std::string zip; }; person p; std::string line; ifstream in("file.txt"); getline(in,line); p.phone_number = line.substr(0,12); p.name = line.substr(13,43); p.address = line.substr(44,144); 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.
•
•
Join Date: Apr 2007
Posts: 15
Reputation:
Solved Threads: 0
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?
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?
>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:
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)
getline(myStringstream, variable_to_extract, '|');
"Technological progress is like an axe in the hands of a pathological criminal."
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.
•
•
Join Date: Apr 2007
Posts: 15
Reputation:
Solved Threads: 0
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
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 ![]() |
Other Threads in the C++ Forum
- Previous Thread: Simple script parser - how to ?
- Next Thread: passing strings as functions
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






