Wrote a function for a prewritten main and received a seg fault that I can't spot

你离开我真会死。 提交于 2019-12-11 14:52:19

问题


Heres my code and I can't figure out why I keep getting a segmentation fault when I run the main. The main was prewritten and I didn't manipulate it. The function compiles without error and the data file that is scanned in exists in the directory.

double getMin(char csvfile[], char column[])
{
    FILE *csvFile=fopen(csvfile, "r");
    int i=isValidColumn(*column);
    if(i==0)
        return -1.0;

    char line[3001];
    fgets(line, 3001, csvFile);
    double min=100.0;
    char *start=line;

    int chapter=0;
    int count=0;
    int section=0;
    char activity;
    char assign[50];
    char *ptr;
    sscanf(column, "%c%d.%d", &activity, &chapter, &section);
    sprintf(assign, "%d.%d - %c", chapter, section, activity);

    while(1){
        char *token=strsep(&start, ",");
        if(token==NULL)
            break;
        ptr=strstr(token, assign);
        count=count+1;
        if(ptr)
            break;
    }

    double val=0;

    while(fgets(line, strlen(line), csvFile))
    {
        char *token;
        char *start=line;
        char rank[50]={0};
        for(i=0; i<count; i++)
            token=strsep(&start, ",");

        if(isdigit(token[0])) strcpy(rank, token);
        sscanf(rank, "%lf", &val);
        if(val<min) min=val;
    }
    fclose(csvFile);
    return min;
}

Heres the CSV list--

Last name,First name,5.1 - Participation (11),5.2 - Participation (20),5.3 - Participation (3),5.4 - Participation (9),5.5 - Participation (5),5.6 - Participation (8),5.7 - Participation (9),5.8 - Participation (4),5.9 - Participation (5),5.10 - Participation (13),5.11 - Participation (21),5.12 - Participation (7),5.13 - Participation (3),5.14 - Participation (0),5.15 - Participation (0),5.1 - Challenge (0),5.2 - Challenge (9),5.3 - Challenge (0),5.4 - Challenge (14),5.5 - Challenge (2),5.6 - Challenge (0),5.7 - Challenge (8),5.8 - Challenge (0),5.9 - Challenge (2),5.10 -` Challenge (0),5.11 - Challenge (0),5.12 - Challenge (0),5.13 - Challenge (0),5.14 - Challenge (0),5.15 - Challenge (0),5.21 - Lab (10),5.23 - Lab (10),5.25 - Lab (10),5.26 - Lab (10),5.27 - Lab (10)
Aguilar,Maria,100,100,100,100,100,100,100,100,100,100,100,100,100,N/A,N/A,N/A,100,N/A,100,100,N/A,100,N/A,100,N/A,N/A,N/A,N/A,N/A,N/A,100,100,100,100,100
Alvarez,Abel,100,100,100,100,100,100,100,100,100,100,100,100,100,N/A,N/A,N/A,100,N/A,100,100,N/A,100,N/A,100,N/A,N/A,N/A,N/A,N/A,N/A,100,100,100,100,100
Andersen,Ronald,100,100,100,100,100,100,100,100,100,100,100,100,100,N/A,N/A,N/A,100,N/A,100,100,N/A,0,N/A,100,N/A,N/A,N/A,N/A,N/A,N/A,,,,,
Arias,Emory,100,100,100,100,100,100,100,100,100,100,100,100,100,N/A,N/A,N/A,100,N/A,100,100,N/A,100,N/A,100,N/A,N/A,N/A,N/A,N/A,N/A,100,100,100,100,100
Barrera,Lucien,100,100,100,100,100,100,100,100,100,100,100,100,100,N/A,N/A,N/A,100,N/A,92.85714286,50,N/A,75,N/A,50,N/A,N/A,N/A,N/A,N/A,N/A,,,,,

Column is entered as a LETTER Num.Num to represent the activity and section number ex. P5.4

来源:https://stackoverflow.com/questions/58457657/wrote-a-function-for-a-prewritten-main-and-received-a-seg-fault-that-i-cant-spo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!