Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中将文本文件中的一行拆分为单独的变量_C - Fatal编程技术网

在C语言中将文本文件中的一行拆分为单独的变量

在C语言中将文本文件中的一行拆分为单独的变量,c,C,我目前正在开发一个程序,该程序读取名为contractions.txt的文件,其中包含致命交通事故的数据,并根据事故发生的时间将数据组织到三个不同的结构中 文本文件格式: 2 2 1 18 3 1 1 1 1 7 2 1 19 1 1 时间(24小时格式)NUMOF车辆NUMOF死亡人数 txt文件中的几行: 2 2 1 18 3 1 1 1 1 7 2 1 19 1 1 需要注意的是,它们由制表符(/t)分隔 我已经编

我目前正在开发一个程序,该程序读取名为contractions.txt的文件,其中包含致命交通事故的数据,并根据事故发生的时间将数据组织到三个不同的结构中

文本文件格式:

2   2   1
18  3   1
1   1   1
7   2   1
19  1   1
时间(24小时格式)NUMOF车辆NUMOF死亡人数

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)
#包括
#包括
//组织文件数据的结构
结构统计{
int事故、车辆、死亡事故;
双重维哈克拉特;
};
内部主(空){
//声明文件指针
文件*fp;
字符行[256];
//声明文件名
char*filename=“contractions.txt”;
//声明结构变量
结构统计上午;
结构统计下午;
结构统计夜;
//使用fopen函数打开文件,然后将其指定给文件指针
fp=fopen(文件名,“r”);
//如果找不到文件,请退出程序
如果(!fp){
printf(“无法打开文件%s”,文件名);
出口(1);
}//if语句的结尾
//按每行遍历文件,并将数据存储到各自的时间范围内
while(fgets(line,sizeof(line),fp)!=NULL){
//在数组中存储行数据
char*val1=strtok(NULL,“/t”);
char*val2=strtok(NULL,“/t”);
char*val3=strtok(空,“/t”);
//如果时间为6-12,则将数据添加到早晨统计数据中
if(val1=12){
上午9点,事故+=1;
上午。车辆+=val2;
上午。死亡人数+=3;
}//if语句的结尾
//如果时间是13-19,则将数据添加到下午统计中
否则如果(val1=19){
下午。事故+=1;
下午。车辆+=车辆2;
下午。死亡人数+=val3;
}//else if语句的结尾
//如果时间在20-23或0-5之间,则向夜间统计添加数据
else如果((val1=23)| |(val1=5)){
夜间事故+=1;
夜间车辆+=val2;
夜间死亡人数+=val3;
}//else if语句的结尾
}//while循环结束
//关闭文件流
fclose(fp);
//计算每个时间组的车辆/事故率
morning.vehAccRat=morning.vehicles/morning.contracts;
午后.vehAccRat=午后.车辆/午后.事故;
night.vehAccRat=night.vehAccRat/夜间事故;
//按时间组织的输出数据(上午/下午/晚上)
printf(“时间跨度\t事件\tVehicles\t目录\t\tVeh./Acc.\n”);
printf(“----------------------------------------------------------------------------------------\n”);
printf(“晨\t\t%d\t\t%d\t\t%d\t\t%.4f\n”,晨。事故,晨。车辆,晨。死亡,晨。Vehacrat);
printf(“午后\t%d\t\t%d\t\t%d\t\t%.4f\n”,午后。事故,午后。车辆,午后。死亡,午后。Vehacrat);
printf(“夜间\t\t%d\t\t%d\t\t%d\t\t%.4f\n”,夜间.事故,夜间.车辆,夜间.死亡,夜间.车辆救援);
printf(“--------------------------------------------------------------------------------------”;
}//int main的结尾(void)

提前感谢您的帮助,我完全被难住了。

我注意到的一件事是,对strtok的第一次调用应该有标记化字符串,随后的调用应该有空字符串,但您的所有调用都有空字符串。

我想
fscanf的
sscanf
应该是您在这里的朋友

循环可以替换为以下内容,以便将每行的三个值中的每一个读取为整数:

 int val1, val2, val3;
 while(fscanf(fp, "%d%d%d", &val1, &val2, &val3) == 3){

在当前的实现中,您也可以在
line
上使用
sscanf
,但这感觉是不必要的。

第一次在字符串上使用
strtok
时,您需要告诉它实际要标记哪个字符串。只有当它已经标记字符串时,您才能传递它
NULL
。您可以使用
fgets
读取该行,然后是
int itemsRead=sscanf(行,“%d%d%d”、&timeOfCrash、&numofhicles、&numofhicles、&numofhicles)这将消耗掉所有的空白。您可以使用strtok
,但这太过分了。您的代码
if(val1=12)
将指针视为
strtok
将子字符串转换为整数,但它没有这样做,
val1过去是,现在仍然是
char*
指针。