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 ?
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
You don't need a array of 5 ints to store a 5 digit integer. Change the struct to:
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.
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
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.
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
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
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.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>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:
getline(myStringstream, variable_to_extract, '|');
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Also see this example and joe's explaination
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>>am I doing something wrong?
Yes -- the std::string c++ class is not defined in string.h -- that header file declares C character handling functions such as strcmp() strlen(), etc. c++ strings are defined in (without the .h extension).
#include <string>
using std::string;
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
>however its not letting me enter 3 arguments in the getline() function
Pop it into a stringstream, and feed this stringstream as the first parameter for getline().
istringstream iss(line);
getline(iss, data, '|');
And stop using void main.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
i'm trying to use atoi to convert string to int keeps on getting error:
F:\register\Cpp1.cpp(35) : error C2664: 'atoi' : cannot convert parameter 1 from 'class std::basic_string,class std::allocator >' to 'const char *'
for(i=0;!myfile.eof();i++)
{
getline(myfile, line);
iss.clear(); iss << line;
}
iss << line isn't going to cut it. It just grabs the first section of it. I prefer just redeclaring the stringstream each loop.
This loop is completely pointless:
for(j=0;getline(iss, data, '|');j++)
{
if(j==0) { item[i].phone=data;}
if(j==1) { item[i].name=data;}
if(j==2) { item[i].addr=data;}
if(j==3) { item[i].zip=atoi(data);}
}
Why not just read each item in like you would normally do? The loop sure isn't simplifying things.
Lastly -- the reason you got the error with atoi is because you were sending it a std::string, you'll have to use string::c_str() if you want to pass it to atoi().
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
>>where do I put "string::c_str()" in my code?
Whereever you need to uncover the C style string hidden in a STL style string object, like when you want to use atoi() to convert an STL style string to an int, etc.
Also, using the return value of eof() to control a loop will get you into trouble sooner or later, so don't do it.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396