Scanf while循环和EOF处理
我有一个必须处理的文本文件,格式为
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
[编辑 #3 - 仅显示整个代码,而不仅仅是编辑以使事情更清晰]
[EDIT #3 - just show the whole code instead of just edits to make things clearer]
您可以重写循环以摆脱其中:
You can re-write your loop to break out of middle of it: