簡體   English   中英

為什么我的C程序產生奇怪的printf輸出?

[英]Why does my C program produce strange printf output?

我的源代碼如下:

#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'}
]

看起來線的末端以某種方式覆蓋了線的開頭。 我究竟做錯了什么?

您的數據文件使用Windows樣式的行結尾( \\r\\n ;回車符+換行符,而不是普通的操作系統' \\n (換行符))。 打印回車符,每次將光標移動到行的開頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM