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