C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Vector of structs

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Neila
Guest





PostPosted: Thu Jan 29, 2004 1:18 am    Post subject: Vector of structs Reply with quote



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


Back to top
Cy Edmunds
Guest





PostPosted: Thu Jan 29, 2004 2:35 am    Post subject: Re: Vector of structs Reply with quote



"Neila" <nospam (AT) me (DOT) com> wrote

Quote:
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::ostream &
operator << (std::ostream &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-9900nzzzx xxxx 999-999-9999n");
std::vector 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

--
Cy
http://home.rochester.rr.com/cyhome/



Back to top
Neila
Guest





PostPosted: Thu Jan 29, 2004 5:40 pm    Post subject: Re: Vector of structs Reply with quote




"Cy Edmunds" <cedmunds (AT) spamless (DOT) rochester.rr.com> wrote

Quote:
// 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

Quote:
void test_user_rec()
{
std::istringstream iss("aaa bbbb 123-678-9900nzzzx xxxx
999-999-9999n");
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



Back to top
Cy Edmunds
Guest





PostPosted: Thu Jan 29, 2004 10:56 pm    Post subject: Re: Vector of structs Reply with quote

"Neila" <nospam (AT) me (DOT) com> wrote

Quote:

"Cy Edmunds" <cedmunds (AT) spamless (DOT) rochester.rr.com> wrote in message
news:ck_Rb.61216$Fe1.50262 (AT) twister (DOT) nyroc.rr.com...
// 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-9900nzzzx xxxx
999-999-9999n");
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.

Quote:

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::ostream &
operator << (std::ostream &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] = '';
ostr << "telephone number: " << buf << 'n';
return ostr;
}

void test_user_rec()
{
std::fstream iss("data.txt");
std::vector 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

--
Cy
http://home.rochester.rr.com/cyhome/



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.