我目前正在开发一个程序,该程序读取一个名为accidents.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)
在此先感谢您的帮助,我完全迷住了。
答案 0 :(得分:0)
我注意到的一件事是,对strtok的第一个调用应具有要标记化的字符串,随后的调用具有NULL,但您所有的调用都具有NULL。
答案 1 :(得分:0)
我认为sscanf
中的fscanf
是您的朋友。
可以将循环替换为以下内容,以将每行三个值分别读取为整数:
int val1, val2, val3;
while(fscanf(fp, "%d%d%d", &val1, &val2, &val3) == 3){
在当前的实现中,您也可以在sscanf
上使用line
,但这感觉不必要。