I am reading information in from a text file that follows this pattern: Id, JobTitle, BasePay, OvertimePay, Benefit, Status. Each part is separated by a tab character. An example of one line is below:
81387 Architectural Associate 114146.55 0 0 FT
However, some lines have base pay missing, and there is instead two tab characters so it would look something like:
81392 Deputy Chief 0 149934.11 FT
When I use strtok with "\t" as the delimiter, it automatically skips over the two tab characters here and goes straight to 0 which is the OvertimePay. Here is the code that I used ("current" is a struct that I'm using to store each piece of info, and ptr has already been defined as a char array):
ptr = strtok(salaryLine, delim); // This gives us ID, which we already have. Skip this
ptr = strtok(NULL, delim);
strcpy(current.title, ptr);
ptr = strtok(NULL, delim);
current.basePay = strtod(ptr, &empty); // This is a problem because it takes OvertimePay instead if BasePay is missing
ptr = strtok(NULL, delim);
current.overtimePay = strtod(ptr, &empty);
ptr = strtok(NULL, delim);
current.benefit = strtod(ptr, &empty);
ptr = strtok(NULL, delim);
strtok(ptr, "\n"); // Remove newline character
strcpy(current.status, ptr);
Is there a way that I could flag strings like that. Specifically, how can I find a string that does not have BasePay in the file? I've tried searching the string for two tab characters next to each other and that doesn't work.
char* temp = strstr(ptr, "\t\t");
if (ptr == NULL)
{
printf("It's null");
}
else
{
printf("It found it");
}
Thank you in advance.
using multi delimiter makes no difference. strtok(ptr, "\t") is the same with strtok(ptr, "\t\t").
In that case I prefer "p2 = strchr(p1, '\t')" loop with a memcpy(s, p1, p2-p1) inside.
BTW, strtok isn't reentrant. use (BTW: reentrant has nothing to do with multi-threading. strtok breaks already with nested loops. One can use strtok_r but it's not as portable.)