free(): invalid pointer Aborted (core dumped) at fclose();

1.1k Views Asked by At

I am trying to call this function two times and when I call it for the second time it gives me this error after printing the row and clm

void datasize(char fl_name[], int* rw, int* clm)
{
    FILE* fl;

    fl = fopen(fl_name, "r+");
    char fldata[100];
    int i = 0, il;
    int j = 0, jl = 0;

    if (fl==NULL)
    {
        printf("%s doesnot exists in the current folder (folder of the executable)\n",fl_name);
    }
    while (fgets(fldata, 100, fl))
    {
        if (fldata[0] > 47 && fldata[0] < 60)
        {
            j = 0;
            char* tokn = strtok(fldata, ",");
            while (tokn)
            {
                if (((tokn[0] > 47) && (tokn[0] < 60)) || tokn[0] == '-')
                {
                    j++;
                }
                tokn = strtok(NULL, ",");
            }
            jl = (j > jl) ? j : jl;
            while (j <= jl)
            {
                j++;
            }
            i++;
        }
    }
    il = i;
    *rw = il;
    *clm = jl;
    printf("\n");
    printf("row : %d clm : %d", il, jl);
    printf("\n");
    fclose(fl);
}

output:

Reading Line Data.....
row : 20 clm : 7
Done
Reading Bus Data.....
row : 14 clm : 9
free(): invalid pointer
Aborted (core dumped)

The part between Reading Data and Done has the function call. We can see that it prints the row and clm but it gives this error at fclose();

I would really appreciate if anyone guide me to the right direction. Thanks.

0

There are 0 best solutions below