Question

So I have a file that has multiple strings. I'm supposed to use fgets to read each line then use sscanf to break the string up and process them into my struct. Here's an example.

38L Lee, Victor; 2.8

The first is the id, second is name, and finally the gpa. When I try using sscanf to read the gpa, it's reading 0.0, rather than 2.8. Here's my code.

bool getstu (FILE* fpstu, STU* pstu)
{

//  Local Definitions
    int ioResult;
    char temp[100];
    char *ptr;
    char tempStr[50];

//  Statements
    fgets(temp, sizeof(temp), fpstu);
    {
        ptr = temp;
        sscanf(ptr, "%3s", pstu->id);
        ptr += strlen(pstu->id) + 1;
        sscanf(ptr, "%[^;]", tempStr);

        pstu->name = aloName(tempStr);

        ptr += strlen(tempStr) + 2;
        sscanf(ptr, "%s", tempStr);
        sscanf(tempStr, "%3.1f", pstu->gpa);
    }

return ioResult == 1;
}// getstu

Can someone explain to me what I'm doing wrong and how I can fix this problem?

Was it helpful?

Solution

sscanf(tempStr, "%3.1f", pstu->gpa);

should be

sscanf(tempStr, "%3.1f", &(pstu->gpa));

, I think.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top