我目前正在开发一个程序,该程序读入名为accident.txt的文件,其中包含致命交通事故的数据,并根据事故发生的时间将数据组织成三种不同的结构。
文本文件格式:
时间(24小时格式)numOfVehicles numOfFatalities
txt文件中的几行:
2 2 1
18 3 1
1 1 1
7 2 1
19 1 1
重要的是要注意它们由制表符(/ t)分隔。
我编写了整个程序,它应该工作,除了读取实际文件并分隔行存储到我的结构中。该计划的整个目标是将每个时间范围内涉及的事故,车辆和死亡事故的总量相加并输出。
while语句中的某些内容,特别是while语句的开头是不正确的。经过几个小时试图找出我做错了什么,我似乎无法弄明白。以下是我的代码。
#include <stdio.h>
#include <stdlib.h>
// Structure for organizing file data
struct stats {
int accidents, vehicles, fatalities;
double vehAccRat;
};
int main(void) {
// Declare file pointer
FILE *fp;
char line[256];
// Declare file name
char* filename = "accidents.txt";
// Declare structure variables
struct stats morning;
struct stats afternoon;
struct stats night;
// Open a file using fopen function then assign it to the file pointer
fp = fopen(filename, "r");
// If file is not found, exit program
if (!fp){
printf("Could not open file %s",filename);
exit(1);
} // End of if statement
// Iterate through file by each line and store data into respective time frame
while (fgets(line, sizeof(line),fp) != NULL) {
// Store line data in array
char *val1 = strtok(NULL, "/t");
char *val2 = strtok(NULL, "/t");
char *val3 = strtok(NULL, "/t");
// If time is from 6 - 12 add data to morning statistics
if (val1 <= 6 && val1 >= 12) {
morning.accidents += 1;
morning.vehicles += val2;
morning.fatalities += val3;
} // End of if statement
// If time is from 13 - 19 add data to afternoon statistics
else if (val1 <= 13 && val1 >= 19) {
afternoon.accidents += 1;
afternoon.vehicles += val2;
afternoon.fatalities += val3;
} // End of else if statement
// If time is from 20 - 23 or 0 - 5 add data to night statistics
else if ((val1 <= 20 && val1 >= 23) || (val1 <= 0 && val1 >= 5)) {
night.accidents += 1;
night.vehicles += val2;
night.fatalities += val3;
} // End of else if statement
} // End of while loop
// Close the file stream
fclose(fp);
// Calculate vehicle / accident ratio for each time group
morning.vehAccRat = morning.vehicles / morning.accidents;
afternoon.vehAccRat = afternoon.vehicles / afternoon.accidents;
night.vehAccRat = night.vehicles / night.accidents;
// Output data organized by time of day (morning/afternoon/night)
printf("Time Span\tAccidents\tVehicles\tFatals\t\tVeh./Acc.\n");
printf("-------------------------------------------------------------------------\n");
printf("Morning\t\t%d\t\t%d\t\t%d\t\t%.4f\n", morning.accidents, morning.vehicles, morning.fatalities, morning.vehAccRat);
printf("Afternoon\t%d\t\t%d\t\t%d\t\t%.4f\n", afternoon.accidents, afternoon.vehicles, afternoon.fatalities, afternoon.vehAccRat);
printf("Night\t\t%d\t\t%d\t\t%d\t\t%.4f\n", night.accidents, night.vehicles, night.fatalities, night.vehAccRat);
printf("-------------------------------------------------------------------------");
} // End of int main(void)
在此先感谢您的帮助,我完全被难过了。
我注意到的一件事是第一次调用strtok应该有字符串标记,然后后续的具有NULL,但你的所有都有NULL。
我认为sscanf
的fscanf
将是你的朋友。
可以用以下代码替换循环,以将每行的三个值中的每一个读取为整数:
int val1, val2, val3;
while(fscanf(fp, "%d%d%d", &val1, &val2, &val3) == 3){
您也可以在当前的实现中使用sscanf
上的line
,但这感觉不必要。