【问题标题】:Scanf while loop and EOF processingScanf while循环和EOF处理
【发布时间】:2011-04-02 04:46:54
【问题描述】:

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

dd - 天 mm - 月 yyyy - 年 x,y,w,z - 温度

dd,mm,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一起使用?

可以假设数据文本文件没有错误或重复条目并且是升序的。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: c null while-loop scanf eof


    【解决方案1】:

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

    已扫描;

    while ((scanned = 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;
    }
    

    【讨论】:

    • @typo.pl 我已经进行了更改,计算代码似乎不起作用,所以我玩弄了||并切换括号但无济于事?
    • 计算代码在哪里?计算代码的变量是否只在while循环中声明?
    • 变量(其他和扫描的)在while循环之外声明。 if 语句位于 while 循环内。我计划在读取总数时计算总数(mm=currentmonth)并在mm!=currentmonth 时输出/显示任何图表等并重置总数。抱歉,如果我不清楚,感谢您的帮助!
    • @typo.pl,哎呀,显示代码当前是未来计算/显示的占位符,以表明我有正确的数据,在这种情况下,两个 printf 行:)
    • 我在 while 循环之后更改了 if 语句条件,试试更新后的代码,看看是否有帮助。
    【解决方案2】:

    您可以重新编写循环以跳出它的中间:

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

    【讨论】:

    • 我们不需要检测任何无效月份,假设数据正确。只是不知道如何更改我的循环,以便它会读取所有数据并处理到 txt 文件中的最后一个月,例如 12 月(或任何其他最后一个月)
    猜你喜欢
    • 1970-01-01
    • 2012-01-07
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 2017-06-15
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多