sscanf to float using c

牧云@^-^@ 提交于 2019-12-12 20:23:36

问题


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?


回答1:


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

should be

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

, I think.



来源:https://stackoverflow.com/questions/15490368/sscanf-to-float-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!