Scanf while循环和EOF处理

时间:2011-04-02 04:46:54

标签: c null while-loop scanf eof

我有一个我们必须处理的文本文件,格式为

dd - day 毫米 - 月 yyyy - 年 x,y,w,z - 温度

DD,毫米,YYYY,X,Y,Z,W

1,1,2010,20.8,19.2,29.3,20.9
2,1,2010,22.5,15.5,30.7,23.3
3,1,2010,21.4,14.5,21.5,18.9
4,1,2010,27.6,13.4,23.9,18.2
5,1,2010,25,16,26.1,18.3
6,1,2010,23.6,16.1,27.6,21.8
...
29,1,2010,23.5,17.5,30.2,19.6
30,1,2010,36.2,13.4,27.3,20.5
31,1,2010,37.2,17.1,26.6,21.5
1,2,2010,24.9,16.9,27.7,22.6
2,2,2010,35.2,16.7,27.7,22.7
3,2,2010,34.8,21.6,27.3,21.4
...
1,12,2010,26.6,16.5,20.1,17.2
2,12,2010,27.2,17.2,24.3,18.5
3,12,2010,30,17.2,24.4,19.8
...
30,12,2010,23.7,14.2,26.5,20
31,12,2010,41.1,14.9,27.2,21.4

我们还没有被教过使用数组来处理这些数据,只有函数,if / else语句,循环(while / for)和操作。

我需要分别处理每个月的数据。我的主要代码包含在while (scanf("%d,%d,%d,%f,%f,%f,%f", &dd, &mm, &yyyy, &x, &y, &z, &w) ==7) {}循环中。

以下是我的代码:

while (scanf("%d,%d,%d,%f,%f,%f,%f", &dd, &mm, &yyyy, &melmax, &melmin, &sydmax, &sydmin) ==7) {
        if (totallinesread==0) {
            currentmonth = mm;

            printf("Stage 4\n=======\n");

        }

        if (mm == currentmonth) {
            daysinmonthcounted+=1;
            /*other totals*/


        }else if (mm !=currentmonth){


            /*can execute code here as needed such as generating graphs etc*/
            printf("0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)\n");
            printf("%d\n", daysinmonthcounted);


            /* make new currentmonth = mm, reset days counted = 1 */
            currentmonth = mm;
            daysinmonthcounted = 1;
        }



        totallinesread+=1;
}

输出:

0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
31
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
28
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
31
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
30
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
31
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
30
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
31
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
31
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
30
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
31
0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)
30
(Note missing december 31 days at the end)

我发现这可以按照我的意愿工作和输出(目前只打印每个月的总天数),但它在上个月完全消失了。我认为它与文件的结尾(EOF)有关,因为if语句没有下一个'mm'值(currentmonth != mm)然后它停止并且不允许我处理任何12月的额外计算代码。

这有什么办法吗?我读过scanf(EOF) = -1,它应与currentmonth != mm一起使用?

可以假设数据文本文件没有错误或重复条目,并且按升序排列。

任何帮助都将不胜感激。

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以重新编写循环以突破其中间:

 while(1) {
   failed = 0;
   if (sscanf(....) != 7) {
     mm = 99; /* invalid month */
     failed = 1;
   };
   /* check for month end */
   if (failed) {
      break;
   };
   ...
 }

答案 1 :(得分:1)

[编辑#3 - 只显示整个代码而不仅仅是编辑以使事情更清晰]

  

int scanning;

     

while((扫描= scanf(“%d,%d,%d,%f,%f,%f,%f”,& dd,& mm,& yyyy,& melmax,& ; melmin,& sydmax,& sydmin))== 7){

   if (totallinesread==0) {
       currentmonth = mm;

       printf("Stage 4\n=======\n");

   }

   if (mm == currentmonth) {
       daysinmonthcounted+=1;
       /*other totals*/


   }else if (mm !=currentmonth){


       /*can execute code here as needed such as generating graphs etc*/
       printf("0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)\n");
       printf("%d\n", daysinmonthcounted);


       /* make new currentmonth = mm, reset days counted = 1 */
       currentmonth = mm;
       daysinmonthcounted = 1;
   }

   totallinesread+=1;

}

if ((scanned == EOF) && (daysinmonthcounted > 1)){

       /*can execute code here as needed such as generating graphs etc*/
       printf("0----+----1----+----2----+----3----+----4----+----5 (x 10, degrees C)\n");
       printf("%d\n", daysinmonthcounted);

       /* make new currentmonth = mm, reset days counted = 1 */
       currentmonth = mm;
       daysinmonthcounted = 1;
}