Scanf while循环和EOF处理

发布于 2024-10-29 22:34:04 字数 2887 浏览 7 评论 0原文

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

dd - day 毫米 - 月 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 一起使用吗?

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

任何帮助将不胜感激。

谢谢你!

I have a text file we must process, in the format

dd - day
mm - month
yyyy - year
x,y,w,z - temperatures

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

We have not been taught to use arrays to process this data, only functions, if/else statements, loops (while/for) and operations.

I need to process the data for each month separately. My main code is contained in a while (scanf("%d,%d,%d,%f,%f,%f,%f", &dd, &mm, &yyyy, &x, &y, &z, &w) ==7) {} loop.

Below is my code:

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;
}

Output:

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)

I am finding that this works and outputs as I wanted to (currently only printing the total days in each month) but it is skipping out on the last month altogether. I figure it has something to do with the end of the file (EOF) and because there is no next 'mm' value for the if statement to use (currentmonth != mm) then it is stopping and not allowing me to process any additional calculation code for December.

Is there some way for this to work? I've read that scanf(EOF) = -1, which should work with currentmonth != mm?

The data text file can be assumed to have no errors or duplicate entries and is in ascending order.

Any help would be appreciated.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏天碎花小短裙 2024-11-05 22:34:04

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

int 扫描;

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

 if (totallinesread==0) {
       当前月份 = 毫米;

       printf("第 4 阶段\n========\n");

   }

   if (mm == 当前月份) {
       月内天数+=1;
       /*其他总计*/


   }否则 if (mm !=当前月份){


       /*可以根据需要在这里执行代码,例如生成图表等*/
       printf("0----+----1----+----2----+----3----+----4----+- ---5 (x 10, 摄氏度)\n");
       printf("%d\n", 月份中的天数);


       /* 新建当前月份 = mm,重置天数 = 1 */
       当前月份 = 毫米;
       月内天数 = 1;
   }

   总行数读取+=1;

}

if ((已扫描 == EOF) && (计数的天数 > 1)){

       /*可以根据需要在这里执行代码,例如生成图表等*/
       printf("0----+----1----+----2----+----3----+----4----+- ---5 (x 10, 摄氏度)\n");
       printf("%d\n", 月份中的天数);

       /* 新建当前月份 = mm,重置天数 = 1 */
       当前月份 = 毫米;
       月内天数 = 1;
}

[EDIT #3 - just show the whole code instead of just edits to make things clearer]

int scanned;

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;
}
横笛休吹塞上声 2024-11-05 22:34:04

您可以重写循环以摆脱其中:

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

You can re-write your loop to break out of middle of it:

 while(1) {
   failed = 0;
   if (sscanf(....) != 7) {
     mm = 99; /* invalid month */
     failed = 1;
   };
   /* check for month end */
   if (failed) {
      break;
   };
   ...
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文