3
我的源代碼如下:爲什麼我的C程序會產生奇怪的printf輸出?
#include <stdio.h>
int main()
{
float latitude;
float longitude;
char info[80];
int started = 0;
puts("data=[");
while (scanf("%f, %f, %79[^\n]", &latitude, &longitude, info) == 3) {
if (started)
printf(",\n");
else
started = 1;
printf("{latitude: %f, longitude: %f, info: '%s'}", latitude, longitude, info);
}
puts("\n]");
return 0;
}
我有一個數據文件gpsdata.csv如下:
42.363400,-71.098465,Speed = 21
42.363327,-71.097588,Speed = 23
42.363255,-71.096710,Speed = 17
我得到
./geo2json < gpsdata.csv
data=[
'},titude: 42.363400, longitude: -71.098465, info: 'Speed = 21
'},titude: 42.363327, longitude: -71.097588, info: 'Speed = 23
{latitude: 42.363255, longitude: -71.096710, info: 'Speed = 17'}
]
它看起來像的結束行是以某種方式覆蓋該行的開頭。我究竟做錯了什麼?
「'%79 [^ \ n]'」沒有做你認爲正在做的事情。 –