Two changes I would make:
char *del = ", "; // test for either space or comma
And (per my comment to the question earlier):
while( fgets(buffer, sizeof(buffer), pdata) != NULL )
{
char *temp;
token = strtok(buffer, del);
if (token != NULL) {
temp = strtok(NULL, del);
if (temp) value1 = atoi( temp );
temp = strtok(NULL, del);
if (temp) {
value2 = atof( temp );
printf("%s is %.3lf\n", token, value1 + value2);
}
}
The strtok
will return NULL if no token is found. Passing a NULL string to atoi
is what's giving you the seg fault, I'm guessing. Like this, we make absolutely sure that doesn't happen. I tested this and it works.