C Seg故障错误:11条已知线路有错误

C Seg故障错误:11条已知线路有错误,c,malloc,token,dynamic-memory-allocation,C,Malloc,Token,Dynamic Memory Allocation,我在这段代码中有一个seg错误,但是我不明白为什么。有人能解释一下我做错了什么吗。 我已经评论了我在这个函数中需要做的事情的列表。我以为我做得对,但当我把它打印出来的时候,却发现实际上发生了完全不同的事情 void analyze_file(FILE *file, struct climate_info **states, int num_states) { const int line_sz = 100; char line[line_sz]; int currentS

我在这段代码中有一个seg错误,但是我不明白为什么。有人能解释一下我做错了什么吗。 我已经评论了我在这个函数中需要做的事情的列表。我以为我做得对,但当我把它打印出来的时候,却发现实际上发生了完全不同的事情

void analyze_file(FILE *file, struct climate_info **states, int num_states) {
    const int line_sz = 100;
    char line[line_sz];
    int currentStates = countStates(states);

    while (fgets(line, line_sz, file) != NULL) 
    {

        char* foundCode = strtok(line, "\t");                   
        int rankOfState = compareOrder(states, foundCode, currentStates);
        if(rankOfState == -1)             
        {

            states[currentStates] = (struct climate_info *) malloc(sizeof(struct climate_info) *num_states);
            strcpy((states[currentStates]) -> code, foundCode);
            states[currentStates] -> num_records=1;

            char* currentTimeStamp = strtok(NULL, "\t");                    
            unsigned long TIMESTAMP;
            sscanf(currentTimeStamp,"%lu", &TIMESTAMP);                        


            char* currentGeol = strtok(NULL, "\t");
            long long GEOL;
            sscanf(currentGeol,"%llu", &GEOL);


            char* currentHumidity = strtok(NULL, "\t");
            double HUMIDITY;
            sscanf(currentHumidity, "%lf",&HUMIDITY);


            char* currentSnow = strtok(NULL, "\t");
            float SNOW;
            sscanf(currentSnow, "%f", &SNOW);


            char* currentCloud = strtok(NULL, "\t");
            double CLOUD;
            sscanf(currentCloud, "%lf",&CLOUD);


            char* currentLightning = strtok(NULL, "\t");
            float LIGHTNING;
            sscanf(currentLightning, "%f", &LIGHTNING);


            char* currentPressure = strtok(NULL,"\t");
            double PRESSURE;
            sscanf(currentPressure, "%lf", &PRESSURE);



            char* currentTemp = strtok(NULL, "\t\n"); 

            double TEMP;
            sscanf(currentTemp, "%lf",&TEMP);


            if (TEMP < states[currentStates]->lo_temp_reading || states[currentStates]->lo_temp_timestamp == 0)
            {
                states[currentStates]->lo_temp_reading = TEMP;
                states[currentStates]->lo_temp_timestamp = TIMESTAMP;
            }
            if (TEMP > states[currentStates]->hi_temp_reading || states[currentStates]->hi_temp_timestamp == 0)
            {
                states[currentStates]->hi_temp_reading = TEMP;
                states[currentStates]->hi_temp_timestamp = TIMESTAMP;
            }

            currentStates++;
        }
        else
        {                                                
            (*(states +rankOfState))->num_records +=1;

            char* currentTimeStamp = strtok(NULL, "\t");
            unsigned long TIMESTAMP;
            sscanf(currentTimeStamp,"%lu", &TIMESTAMP);


            char* currentGeol = strtok(NULL, "\t");
            (*(states +rankOfState))->hi_millitime += *currentGeol;

            char* currentHumidity = strtok(NULL, "\t");
            double HUMIDITY;
            sscanf(currentHumidity, "%lf",&HUMIDITY);
            (*(states +rankOfState))->humidity += HUMIDITY;

            char* currentSnow = strtok(NULL, "\t");
            float SNOW;
            sscanf(currentSnow, "%f", &SNOW);
            (*(states +rankOfState))->snow += SNOW;

            char* currentCloud = strtok(NULL, "\t");
            double CLOUD;
            sscanf(currentCloud, "%lf",&CLOUD);
            (*(states +rankOfState))->cloud += CLOUD;

            char* currentLightning = strtok(NULL, "\t");
            float LIGHTNING;
            sscanf(currentLightning, "%f", &LIGHTNING);
            (*(states +rankOfState))->lightning += LIGHTNING;

            char* currentPressure = strtok(NULL,"\t");
            double PRESSURE;
            sscanf(currentPressure, "%lf", &PRESSURE);
            (*(states +rankOfState))->pressure += PRESSURE;

            char* currentTemp = strtok(NULL, "\t\n");
            double TEMP;
            sscanf(currentTemp, "%lf",&TEMP);

            (*(states +rankOfState))->temperature += TEMP;

            if (TEMP <= states[currentStates]->lo_temp_reading)
            {
                states[currentStates]->lo_temp_reading = TEMP;
                states[currentStates]->lo_temp_timestamp = *currentTimeStamp;
            }
            else if (*currentTemp > states[currentStates]->hi_temp_reading)
            {
                states[currentStates]->hi_temp_reading = *currentTemp;
                states[currentStates]->hi_temp_timestamp = *currentTimeStamp;
            }

            currentStates++;
        }
    }
}
void analyze_文件(文件*文件,结构气候信息**状态,整数状态){
const int line_sz=100;
字符行[line_sz];
int currentStates=countStates(状态);
while(fgets(行、行、文件)!=NULL)
{
char*foundCode=strtok(第“\t”行);
int rankOfState=compareOrder(状态、foundCode、CurrentState);
if(rankOfState==-1)
{
状态[当前状态]=(结构气候信息*)malloc(sizeof(结构气候信息)*num_状态);
strcpy((状态[当前状态]->代码,foundCode);
状态[CurrentState]->num_记录=1;
char*currentTimeStamp=strtok(空,“\t”);
无符号长时间戳;
sscanf(当前时间戳、%lu、&TIMESTAMP);
char*currentGeol=strtok(空,“\t”);
长土工土;
sscanf(当前GEOL、%llu、&GEOL);
char*current湿度=strtok(空,“\t”);
双湿度;
sscanf(当前湿度、%lf)和湿度);
char*currentSnow=strtok(空,“\t”);
飘雪;
sscanf(当前雪、%f、&SNOW);
char*currentCloud=strtok(空,“\t”);
双云;
sscanf(当前云、%lf、&CLOUD);
char*currentLightning=strtok(空,“\t”);
浮雷;
sscanf(当前闪电、%f、&LIGHTNING);
char*currentpression=strtok(空,“\t”);
双重压力;
sscanf(当前压力、%lf)和压力);
char*currentTemp=strtok(空,“\t\n”);
双温;
sscanf(当前温度、%lf、&TEMP);
如果(温度<状态[currentStates]->低温度读数| |状态[currentStates]->低温度时间戳==0)
{
状态[当前状态]->低温度读数=温度;
状态[currentStates]->lo_temp_timestamp=时间戳;
}
如果(温度>状态[currentStates]->hi_TEMP_读数| |状态[currentStates]->hi_TEMP_时间戳==0)
{
状态[当前状态]->高温度读数=温度;
状态[当前状态]->高温时间戳=时间戳;
}
currentStates++;
}
其他的
{                                                
(*(states+rankOfState))->num_记录+=1;
char*currentTimeStamp=strtok(空,“\t”);
无符号长时间戳;
sscanf(当前时间戳、%lu、&TIMESTAMP);
char*currentGeol=strtok(空,“\t”);
(*(状态+rankOfState))->hi_毫秒+=*currentGeol;
char*current湿度=strtok(空,“\t”);
双湿度;
sscanf(当前湿度、%lf)和湿度);
(*(状态+状态))->湿度+=湿度;
char*currentSnow=strtok(空,“\t”);
飘雪;
sscanf(当前雪、%f、&SNOW);
(*(州+州))->雪+=雪;
char*currentCloud=strtok(空,“\t”);
双云;
sscanf(当前云、%lf、&CLOUD);
(*(州+州))->cloud+=云;
char*currentLightning=strtok(空,“\t”);
浮雷;
sscanf(当前闪电、%f、&LIGHTNING);
(*(州+州))->闪电+=闪电;
char*currentpression=strtok(空,“\t”);
双重压力;
sscanf(当前压力、%lf)和压力);
(*(状态+状态))->压力+=压力;
char*currentTemp=strtok(空,“\t\n”);
双温;
sscanf(当前温度、%lf、&TEMP);
(*(状态+状态))->温度+=温度;
if(温度低温度读数)
{
状态[当前状态]->低温度读数=温度;
状态[currentStates]->lo_temp_timestamp=*currentTimeStamp;
}
否则如果(*currentTemp>状态[currentStates]->高温读数)
{
状态[当前状态]->高温度读数=*当前温度;
状态[currentStates]->hi_temp_timestamp=*currentTimeStamp;
}
currentStates++;
}
}
}
如果我注释掉这些行,我的输出会打印出来,但它只分析一行

if (TEMP <= states[currentStates]->lo_temp_reading)
{
    states[currentStates]->lo_temp_reading = TEMP;
    states[currentStates]->lo_temp_timestamp = *currentTimeStamp;
}
else if (*currentTemp > states[currentStates]->hi_temp_reading)
{
   states[currentStates]->hi_temp_reading = *currentTemp;
   states[currentStates]->hi_temp_timestamp = *currentTimeStamp;
}
if(温度低温度读数)
{
状态[当前状态]->低温度读数=温度;
状态[currentStates]->lo_temp_timestamp=*currentTimeStamp;
}
否则如果(*currentTemp>状态[currentStates]->高温读数)
{
状态[当前状态]->高温度读数=*当前温度;
状态[currentStates]->hi_temp_timestamp=*currentTimeStamp;
}
以下是我使用的帮助器函数:

int compareOrder(struct climate_info **states, char codex[3], int currentStates)          //returns the order of each state in the array
{
    int order = 0;
    while (order < currentStates)          //while order is less than number of states analyzed
    {
        if(strcmp((states[order])->code, codex) == 0)       //if the states is present
        {
            return order;
        }
        order++;                                                //increment here to check every line for when to update state codes
    }
    return -1;                                                  //returns -1 the state is not prsent in struct
}


int countStates(struct climate_info **states)                           //function to count number of states present
{
    int num = 0;
    while(num < 50 && states[num] != NULL)
    {
        num++;
    }
    return num;
}
int compareOrder(struct climate_info**states,char codex[3],int currentStates)//返回数组中每个状态的顺序
{
整数阶=0;
while(ordercode,codex)==0)//如果存在状态
{
退货单;
}
order++;//在这里递增以检查每一行何时更新状态代码
}
return-1;//返回-1结构中的状态不是prsent
}
int countStates(结构气候信息**状态)//fu
size_t countStates(struct climate_info** states)
{
    struct climate_info** end = states;
    while(*end)
        ++end;
    return end - states;
}
size_t countStates(struct climate_info** states, size_t length)
{
    struct climate_info** end = states;
    while(length-- && *end)
        ++end;
    return end - states;
}
// (size_t is more appropriate than int here...)
void analyze_file(FILE* file, struct climate_info* states[], size_t num_states)
{
    const size_t line_sz = 100;
    char line[line_sz];
    size_t currentStates = countStates(states, num_states);

    while (fgets(line, line_sz, file) != NULL) 
    {
        char* foundCode = strtok(line, "\t");                   
        int rankOfState = compareOrder(states, foundCode, currentStates);
        if(rankOfState == -1)             
        {
            // new states available at all?
            if(currentStates == num_states)
            {
                 // some appropriate error handling - need to decide you!
                 // for now, just returning from function:
                 return;
            }

            // just allocate one struct (assumption): 
            states[currentStates] = (struct climate_info *) malloc(sizeof(struct climate_info));

            // always check the result of malloc!
            if(!states[currentStates])
            {
                 // allocation failed, no memory available on OS!!!
                 // some appropriate error handling - need to decide you!
                 // for now, just returning from function:
                 return;
            }

            // prefer strncpy to assure you don't copy past the end!
            // sure, it will fill overdue bytes with 0, but still we are safer
            // (assumption: climate_info contains an array!)
            strncpy(states[currentStates]->code, foundCode, sizeof(states[currentStates]->code));
            states[currentStates]->num_records = 1;

            // ...

            // now you created a new struct with malloc; be aware
            // that memory is uninitialized and could contain *ANY*
            // data, reading uninitialized memory is undefined behaviour!
            //if (TEMP < states[currentStates]->lo_temp_reading || states[currentStates]->lo_temp_timestamp == 0)
            //{
                states[currentStates]->lo_temp_reading = TEMP;
            //    states[currentStates]->lo_temp_timestamp = TIMESTAMP;
            //}
            //if (TEMP > states[currentStates]->hi_temp_reading || states[currentStates]->hi_temp_timestamp == 0)
            //{
                states[currentStates]->hi_temp_reading = TEMP;
                states[currentStates]->hi_temp_timestamp = TIMESTAMP;
            //}
            // (it's a new set anyway, so just set the values...)

            currentStates++;
        }
        else
        {                                                
            ++states[rankOfState]->num_records; // just a little bit simpler...

            // ...

            states[rankOfState]->temperature += TEMP;

            if (TEMP <= states[currentStates]->lo_temp_reading)
            {
                states[currentStates]->lo_temp_reading = TEMP;
                states[currentStates]->lo_temp_timestamp = *currentTimeStamp;
            }
            // this should not have resulted in crash, but assign a bad value!
            else if (TEMP /* *currentTemp */ > states[currentStates]->hi_temp_reading)
            {
                states[currentStates]->hi_temp_reading = TEMP /* *currentTemp */;
                states[currentStates]->hi_temp_timestamp = *currentTimeStamp;
            }
            // same for timestamp in both branches: you don't want to assign first character of string,
            // but the parsed value (e. g. 1012, if string was "1012", *... would deliver 49
            // (ASCII code of character `1`; assuming you have ASCII compatible encoding)

            // wrong in this branch: you did NOT add a new element
            //currentStates++;
        }
    }
}