在多行输入中扫描字符串-C语言

时间:2018-09-18 07:45:12

标签: c string

我得到以下输入:

10/9/02 9:15:59 23.845089 38.018470 DXUYHu

10/9/02 9:16:29 23.845179 38.018069 tKoPTx

10/9/02 9:16:59 23.845530 38.018241 JPQbNb

10/9/02 9:17:29 23.845499 38.017440 aEWdXS

10/9/02 9:17:59 23.844780 38.015609 gqeEjx

10/9/02 9:18:29 23.844780 38.014018 aQArkX

10/9/02 9:18:59 23.844869 38.012569 fhQIAS

10/9/02 9:19:29 23.845360 38.011600 BhngfQ

10/9/02 9:19:59 23.845550 38.010650 rgwehm

10/9/02 9:20:29 23.845100 38.010478 jdBgpN

并且我正在尝试开发一个代码,该代码读取最后的6个字符-每行的字符串,然后将其打印出来。出于某种原因,我开发的代码仅会打印出最终字符串'jdBgpN',请提供原因。

for (i = 0; i <= LASTROW-1; i++ ) {
    scanf("%d/%d/%d %d:%d:%d %lf %lf %s",&day, &month, &year, 
           &hour, &minute, &second, &longi, &lati, id);`
}

for (i=0; i < LASTROW; i++) {
    scanf("%s", id[i]);
}

for (i=0; i < LASTROW; i++) {
    printf("%s\n", id[i]);
}

1 个答案:

答案 0 :(得分:1)

在第一个for循环中,您覆盖了ID,因此您仅保留一个值

for (i = 0; i <= LASTROW-1; i++ ) {
    scanf("%d/%d/%d %d:%d:%d %lf %lf %s",&day, &month, &year, 
           &hour, &minute, &second, &longi, &lati, id);`
}

假设id是一个数组,则可以将数组变量视为指针,并且可以将索引添加到变量以沿错误移动。

for (i = 0; i <= LASTROW-1; i++ ) {
    scanf("%d/%d/%d %d:%d:%d %lf %lf %s",&day, &month, &year, 
           &hour, &minute, &second, &longi, &lati, id + i);`
}