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?

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 ?

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.

commented: Great helper!!! +1

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.

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.

commented: dude knows his sheet!! +1

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!

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?

>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, '|');

Also see this example and joe's explaination

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):Statement 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):Structure passed by value

>>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 <string> (without the .h extension).

#include <string>
using std::string;

I'm having all kinds of problems today ... now its not letting me use getline ....any ideas?

I've included <iostream> and <string>

Error: register.cpp(8,20):Could not find a match for 'std::getline(istream_withassign,std::basic_string<char,std::string_char_traits<char>,std::allocator<char>>)'

Post the code.

I figured that last problem out... however its not letting me enter 3 arguments in the getline() function.
here's my code:

#include <stdio.h>
#include <fstream>
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
void main(void)
{
    string line,data;
    ifstream myfile ("inv.txt");
    while (! myfile.eof() ) 
        {
        getline (myfile,line);
        while (line != " ")         {          getline(line,data, '|');   /* referance A */     }
        cout<< line << endl;    
    }
    myfile.close();
    getch();
    }
}

here's the data file:

425-697-3142|john smith|123 main street #517|83210
313-510-2262|buddy jack|56 south main street|04210
415-121-2527|rudy shmidt|1456 green street|20210
512-913-4289|lacy park|556 blue street|90214


at referance point A I'm trying to scan the line until it reads the field delemiter '|' and store the data in string data.

>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.

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<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'

for(i=0;!myfile.eof();i++)    
        {
        getline(myfile, line);
        iss.clear(); iss << line;
        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);}
            }
        }

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<char,struct std::char_traits<char>,class std::allocator<char> >' 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().

where do I put "string::c_str()" in my code?

>>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.

when you say "string::c_str()" do you mean stringname::c_str() or just the exact same line?
I've tried both of them in my program and neither one works....i get werrors like:

C:\Documents and Settings\user\Desktop\c\Cpp1.cpp(30) : error C2039: 'plu' : is not a member of 'inventory'

Code

for(i=0;!myfile.eof();i++)    
    {
    cout <<i<< " ";    
    getline(myfile, line);
    iss.clear();    
    iss << line;
    cout << line << endl << endl;
    for(j=0;getline(iss, data, '|');j++)                
    {
        item[i].plu::c_str();
        if(j==0) { item[i].plu=atoi(data);}
        if(j==1) { item[i].disc=data;}
        if(j==2) { item[i].tax=data;}
        if(j==3) { item[i].price=data;}
        cout << j << " " << data << endl;
    }
    }/code]

when i use; string::c_str();
I get the error:

C:\Documents and Settings\user\Desktop\c\Cpp1.cpp(30) : error C2352: 'std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::c_str' : illegal call of non-static member function

Here's how you use c_str:

a_function_that_needs_cstr( my_string.c_str() ) ;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.