문제

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?

도움이 되었습니까?

해결책

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

should be

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

, I think.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top