sscanf没有正确扫描字符串中的元素

时间:2017-08-12 01:50:37

标签: c string

所以我写了一个像这样的短节目

#include <stdio.h>
#include <stdlib.h> 
#include <math.h>
#include <string.h>

const double PI = 3.14159;

int main()
{
    char str[120];
    double distance, azimuth;
    double delta_x = 0;
    double delta_y = 0;

    FILE *fp = fopen("input.txt", "r");
    if (fp == NULL)
        return -1;
    fgets(str, sizeof(str), fp); //skip first line

    while (fgets(str, sizeof(str), fp)) {
        if (strcmp("Dig here!", str) == 0) 
            break;
        printf(str);
        sscanf(str, "go %f feet by azimuth %f\n", &distance, &azimuth);
        printf("distance %f azimuth %f\n", distance, azimuth);
        delta_y += distance * sin(azimuth * (PI/180));
        delta_x += distance * cos(azimuth * (PI/180));
    }

    printf("%d %d", delta_x, delta_y);

    fclose(fp);
    return 0;
}

input.txt 看起来像这样

Stand at the pole with the plaque START

go 500 feet by azimuth 296

go 460 feet by azimuth 11

Dig here!

然而输出

go 500 feet by azimuth 296

distance 0.000000 azimuth 0.000000

我确定这是一个我错过的愚蠢错误,但我似乎无法找到它,任何帮助将不胜感激。

0 个答案:

没有答案