Vector of structs

N

Neila

I have a file that includes several records each composed of a first name,
last name and telephone number (each field separated with a space) which
looks something like this

aaa bbbb 123-678-9900
zzzx xxxx 999-999-9999
etc...

I have a structure defined as

struct user_rec
{
char fname[25]
char lname[25]
char telno[12]
}

What I want to do is have a vector of type user_rec that will store each
record. The number of record in a file is unknow that is why I'm using
dynamic allocation.

I'm trying to treat each line of the file as a string and then tokenize it
using the space as the delimiter

//FileIN is my file that I opened
//ptrToken is the tokenizer
//v_Line is the array that will hold each record to be tokenized

vector<user_rec> users_info;

while (!FileIN.eof())
{
//get each line and tokenize
FileIN.getline(v_Line,C_LNG_LINE, '\n');

ptrToken= strtok ( v_Line, C_DELIM );


//Now I'm stuck here, how do I insert into a struct? and
how do I move the //token to the next word in a
record?

users_info.fname.push_back(ptrToken);
ptrToken = strtok ( NULL, C_DELIM );

users_info.lname.push_back(ptrToken);
ptrToken = strtok ( NULL, C_DELIM );

users_info.telno.push_back(ptrToken);
ptrToken = strtok ( NULL, C_DELIM );



}

would there be any better solutions then strtok ? (Maybe read character by
character?)
thanks for any help
 
C

Cy Edmunds

Neila said:
I have a file that includes several records each composed of a first name,
last name and telephone number (each field separated with a space) which
looks something like this

aaa bbbb 123-678-9900
zzzx xxxx 999-999-9999
etc...

I have a structure defined as

struct user_rec
{
char fname[25]
char lname[25]
char telno[12]
}

What I want to do is have a vector of type user_rec that will store each
record. The number of record in a file is unknow that is why I'm using
dynamic allocation.

I'm trying to treat each line of the file as a string and then tokenize it
using the space as the delimiter

//FileIN is my file that I opened
//ptrToken is the tokenizer
//v_Line is the array that will hold each record to be tokenized

vector<user_rec> users_info;

while (!FileIN.eof())
{
//get each line and tokenize
FileIN.getline(v_Line,C_LNG_LINE, '\n');

ptrToken= strtok ( v_Line, C_DELIM );


//Now I'm stuck here, how do I insert into a struct? and
how do I move the //token to the next word in a
record?

users_info.fname.push_back(ptrToken);
ptrToken = strtok ( NULL, C_DELIM );

users_info.lname.push_back(ptrToken);
ptrToken = strtok ( NULL, C_DELIM );

users_info.telno.push_back(ptrToken);
ptrToken = strtok ( NULL, C_DELIM );



}

would there be any better solutions then strtok ? (Maybe read character by
character?)
thanks for any help

You might try this:

#include <string>
#include <iostream>
#include <sstream>
#include <vector>

// use std::string rather than char[]
struct user_rec
{
std::string fname;
std::string lname;
std::string telno;
};

// define how to read a user_rec
std::istream &
operator >> (std::istream &istr, user_rec &ur)
{
istr >> ur.fname >> ur.lname >> ur.telno;
return istr;
}

// define how to write a user_rec
std::eek:stream &
operator << (std::eek:stream &ostr, const user_rec &ur)
{
ostr << "first name: " << ur.fname << '\n';
ostr << "last name: " << ur.lname << '\n';
ostr << "telephone number: " << ur.telno << '\n';
return ostr;
}

void test_user_rec()
{
std::istringstream iss("aaa bbbb 123-678-9900\nzzzx xxxx 999-999-9999\n");
std::vector<user_rec> v;
user_rec dummy;
while (true)
{
iss >> dummy;
if (!iss)
break; // presumably out of data
v.push_back(dummy);
}
for (std::vector<user_rec>::const_iterator i = v.begin(); i != v.end();
++i)
std::cout << *i;
}

Output is:

first name: aaa
last name: bbbb
telephone number: 123-678-9900
first name: zzzx
last name: xxxx
telephone number: 999-999-9999
 
N

Neila

Cy Edmunds said:
// use std::string rather than char[]
struct user_rec
{
std::string fname;
std::string lname;
std::string telno;
};


Unfortunalty I need to keep the structure of char

And I'm a bit puzzled with this
void test_user_rec()
{
std::istringstream iss("aaa bbbb 123-678-9900\nzzzx xxxx 999-999-9999\n");
std::vector<user_rec> v;
user_rec dummy;

how would I go about getting only one line of a file at a time and then
innserting it into the structure and the structure into the vector?

thanks anyway for your help
 
C

Cy Edmunds

Neila said:
Cy Edmunds said:
// use std::string rather than char[]
struct user_rec
{
std::string fname;
std::string lname;
std::string telno;
};


Unfortunalty I need to keep the structure of char

And I'm a bit puzzled with this
void test_user_rec()
{
std::istringstream iss("aaa bbbb 123-678-9900\nzzzx xxxx 999-999-9999\n");
std::vector<user_rec> v;
user_rec dummy;

how would I go about getting only one line of a file at a time and then
innserting it into the structure and the structure into the vector?

What's a "line"? '\n' is just white space.
thanks anyway for your help

I used a hack (istringstream) to avoid having to type in an actual data
file. Sorry if I confused you. Let's try it again:

struct user_rec
{
char fname[25];
char lname[25];
char telno[12];
};

std::istream &
operator >> (std::istream &istr, user_rec &ur)
{
std::string buffer;
istr >> buffer;
strncpy(ur.fname, buffer.c_str(), 25);
istr >> buffer;
strncpy(ur.lname, buffer.c_str(), 25);
istr >> buffer;
strncpy(ur.telno, buffer.c_str(), 12);
return istr;
}

std::eek:stream &
operator << (std::eek:stream &ostr, const user_rec &ur)
{
ostr << "first name: " << ur.fname << '\n';
ostr << "last name: " << ur.lname << '\n';
char buf[13]; // compensate for short telno field
strncpy(buf, ur.telno, 12);
buf[12] = '\0';
ostr << "telephone number: " << buf << '\n';
return ostr;
}

void test_user_rec()
{
std::fstream iss("data.txt");
std::vector<user_rec> v;
user_rec dummy;
while (true)
{
iss >> dummy;
if (!iss)
break;
v.push_back(dummy);
}
iss.close();
for (std::vector<user_rec>::const_iterator i = v.begin(); i != v.end();
++i)
std::cout << *i;
}

The file "data.txt" contains:

aaa bbbb 123-678-9900
zzzx xxxx 999-999-9999

Output is (once again):

first name: aaa
last name: bbbb
telephone number: 123-678-9900
first name: zzzx
last name: xxxx
telephone number: 999-999-9999
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top