所以香港专业教育学院写了一个小程序这样sscanf的不是从字符串中的元素扫描正常
#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
我敢肯定,这是一个愚蠢的错误,我失踪,但我似乎无法找到它,任何帮助,将不胜感激。
使用'%lf'代替'%F'为'sscanf'中的'double'。 – BLUEPIXY
@BLUEPIXY修复它谢谢!你介意告诉我为什么这是有效的吗? – Mitchel0022
阅读[sscanf](http://en.cppreference。com/w/c/io/fscanf) – BLUEPIXY