0

I've been stuck on this project for the better part of the week and while I have been grinding through syntax errors.

The gist of the project is to read through the headers and stopping at a certain column and finding certain characteristics like the min, max and average of the column. However, I am having a tough time with memory allocation for this job. I have been unable to further my code so far because I keep getting an error labeled Segmentation Fault: 11.

The bolded section is what I have narrowed the problem down to. I know there is a memory problem but I cannot see why fgets is not reading in the first line of the file from within the function. The file does not return NULL either and is able to be accessed. Dynamic arrays are an option but I am a beginner and very unfamiliar with them.


double getMin(char csvfile[], char column[])
{
    int counter= 0;
    int counter2= 0;
    char* token;
    char type;
    char activity[20];
    int chap, sec;
    int min = -1000;
    char header[20];
    char *res;
char row[3000];
char Toprow[3000];

    sscanf(column, "%c%d.%d", &type, &chap, &sec);
    if (type!='P' && type!='C' && type!='L') return -2.0;
    if (chap<=0 || sec<=0) return 0;

    if(type == 'c' || type == 'C'){
        sprintf(activity,"%challange", type);
    }
    else if(type == 'P' || type == 'p'){
        sprintf(activity,"%carticipation", type);
    }
    else {
        sprintf(activity,"%cab", type);
    }

    sprintf(header, "%d.%d - %s", chap, sec, activity);

    FILE*  inFile = NULL;
   inFile = fopen("array.csv","r"); 
    //This is where the trouble begins that Ive narrowed down to 

    while(!feof(inFile)){
        fgets(Toprow, 3000, inFile);
        while(token != NULL){
            counter = counter + 1;
            token = strsep(&Toprow, ",");
            printf("%s", token);
            res = strstr(token, header);
            if(res){
            break;
                token = NULL;
                }
            }
    } 
        while (!feof(inFile)) {
            int currVal= 0;
            fgets(row, 3000, inFile);
            if(feof(inFile)) break;
            while( (token = strsep(&row, ','))  != NULL){
                ++counter2;
                    if(counter2 == counter){
                        res = strstr(token, header);
                        if(!res){
                        int currVal = atoi(*token);
                            if(currVal > min)
                                min = currVal;
                        counter2 = 0;
                        break;
                        }
                    }

                }
        }
    return min;
}

The excel file looks like this for reference enter image description here

Obsidian
  • 3,719
  • 8
  • 17
  • 30
  • Someone posted a very similar program earlier this afternoon. – Barmar Oct 18 '19 at 21:23
  • Not necessarily your problem, but see [Why is `while(!feof(file))` always wrong?](https://stackoverflow.com/questions/5431941) – Steve Summit Oct 18 '19 at 21:25
  • The similar code is in https://stackoverflow.com/questions/58457657/wrote-a-function-for-a-prewritten-main-and-received-a-seg-fault-that-i-cant-spo – Barmar Oct 18 '19 at 21:29
  • @SteveSummit This code is checking `feof()` in the wrong place *and* the right place. – Barmar Oct 18 '19 at 21:30
  • The call `token = strsep(&Toprow, ",")` looks pretty wrong. I suspect it should be `token = strsep(Toprow, ",")`. (P.S. I didn't spot this myself, my compiler did. Do yourself a favor and find a modern compiler that warns about mistakes like these. That probably could have shaved half a week off the time you've spent, right there) – Steve Summit Oct 18 '19 at 21:31
  • This probably won't be a helpful comment, but: I'd say this code is beyond repair. Whatever it's trying to do is so tangled up that it would likely be difficult for an expert -- let alone a beginner -- to find and fix its bugs. Even if it can be made to work, it will never be maintainable or extensible or any of the other ibles that makes good code good. – Steve Summit Oct 18 '19 at 21:36
  • I would suggest starting from scratch, in three stages: (1) write a program that opens the file, reads lines from it using `fgets`, and prints them back out. Get that program working **perfectly** -- it's the foundation for what follows. (2) find or write some code to parse one line of CSV. Either construct an array of pointers, one for each field on the line, or devise a clean -- very clean -- structure for iterating over the fields on the line. Get that program working **perfectly** -- it's the foundation for what follows. – Steve Summit Oct 18 '19 at 21:36
  • Finally, (3) start operating on the fields or columns or rows you care about, computing maxima or minima or whatever else you have to do. There are lots of good ways to do that -- but unfortunately, what you have here now isn't one of them Do a web search on "parsing CSV files" -- there have got to be gobs of tutorials out there showing you how to do it. It's an at least mildly hard problem, and one you shouldn't have to solve from scratch. – Steve Summit Oct 18 '19 at 21:40
  • From the [man pages for strsep](http://man7.org/linux/man-pages/man3/strsep.3.html) **In case no delimiter was found, the token is taken to be the entire string *stringp, and *stringp is made NULL.**. May be something like that is going on... – Ravindra HV Oct 18 '19 at 22:59

0 Answers0