Hi all,
I am trying to read from a file that contains string records and copy the elements to a struct upon startup of the program. Each record is on one line and the elements of the record are separated by the delimeter ";". The problem is that some elements, like "name" have two parts (eg. john smith;labourer;555-5555).
I am using strtok to separate the strings. Here is the basic code that performs this:
Code:
while (!b_file.eof()) {
b_file>>str;
char * pch;
pch = strtok (str,";");
int var = 0;
while (pch!=NULL)
{
if (var == 0){
strcpy(emp[numEmployees].name, pch);
}
if (var == 1){
strcpy(emp[numEmployees].department, pch);
}
if (var == 2){
strcpy(emp[numEmployees].job, pch);
}
if (var == 3){
strcpy(emp[numEmployees].phone, pch);
}
var++;
pch = strtok (NULL, ";");
}
numEmployees++;
}
If I save the name as "John" instead of "John Smith" the code works fine. If the name has 2 parts then it creates a new record in the struct containing the remaining parts of the record.
eg:
john null null
smith labourer 555-5555
I was under the impression that strtok tokenized only on the delimeter that I specidied, but it seems to also tokenize on the space by default.
Does anyone have any quick and clean solutions to this problem?
Thanks,
snowblind37