0

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

Barmar
  • 741,623
  • 53
  • 500
  • 612
CHuck
  • 11
  • 2
  • 1
    Welcome to Stack Overflow! [Why `while(!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Oct 18 '19 at 20:21
  • `if(token=='\0')` is better written as `if (token == NULL)` – Barmar Oct 18 '19 at 20:22
  • The `while(!feof(csvFile))` loop doesn't read from the file. – Barmar Oct 18 '19 at 20:24
  • That should be `while(fgets(line, sizeof line, csvFile)` – Barmar Oct 18 '19 at 20:26
  • if `isdigit(token[0])` is true, then all the other tests on that line will necessarily be true, so they're redundant. – Barmar Oct 18 '19 at 20:28
  • Thanks Barmar! Unfortunately I'm still getting the seg fault after making those corrections – CHuck Oct 18 '19 at 20:31
  • Which line is the seg fault happening on? – Barmar Oct 18 '19 at 20:39
  • Can you show examples of the CSV file contents, and what `column` is? – Barmar Oct 18 '19 at 20:42
  • Put it in the question so you can format it properly. – Barmar Oct 18 '19 at 20:47
  • is that better? – CHuck Oct 18 '19 at 20:53
  • `strlen(line)` should be `sizeof line` – Barmar Oct 18 '19 at 20:56
  • After you call `strsep()`, `strlen(line)` is the length of the first field of the line. – Barmar Oct 18 '19 at 20:58
  • changed it, still got that dang seg fault – CHuck Oct 18 '19 at 21:03
  • You still haven't answered my question: which line is the segfault happening on? The debugger should be able to tell you where it's happening. – Barmar Oct 18 '19 at 21:05
  • You can also step through the program with the debugger. – Barmar Oct 18 '19 at 21:05
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Barmar Oct 18 '19 at 21:06
  • In all seriouness trying to find the error in this much code without any localization is silly (and just think how silly it would be when you have ten times as much code...). Fire up the debugger, learn what line is generating the fault, *then* start asking what bit of code might be causing it. – dmckee --- ex-moderator kitten Oct 18 '19 at 21:06
  • error says Segmentation fault: 11 – CHuck Oct 18 '19 at 21:07
  • Yes. On a unix derived system a segmentation fault is signal 11. That is not what people are asking you. They are asking *what part of the program* failed. You won't get that information by just running the program. You have to run it with a tool designed to give you that information (and a wealth of other good stuff). The tool is called a "debugger". Your development environment has one even if you don't know about it yet. Now is a good time to start learning about it. – dmckee --- ex-moderator kitten Oct 18 '19 at 21:11
  • Discussion on some very similar-looking code [here](https://stackoverflow.com/questions/58458345/segmentation-fault-while-trying-to-parse-an-excel-style-csv-file-c-programming). – Steve Summit Oct 18 '19 at 21:51

0 Answers0