0

I have create a struct and it has its id number, its value, and its status. I have a file that consist of the data(1 199 0 2 199 1...) 1 its the number, 199 is the value, 0 is the status and keep going like this... I have used 1 function called filldata() to read 3 numbers at a time, which are for example, 1 199 0 and then put it into the passed element of a struct array. And then, i used another function to call the this function to fill up the struct array. The fillAll function will return the set of data tha.t had been copied from the file to the struct array But i received a segmentation fault. Any idea why? The codes explain better:

int filldata(struct Data_point *a, const char *filelocation)  
    {

        FILE *f;
        if((f=fopen(filelocation,"r"))==NULL)
            printf("You cannot open");

        if( fscanf(f, "%ld%lf%d", &(a->sampleNumber), &(a->value), &(a->status)) == 3)
            return 1;   
        else
            return 0;
    }

    int fillAll(struct Data_point *a, const char *filelocation)// I will pass the struct array and the location of my file string
    {
        int index=0;
        while(filldata(&a[index], filelocation))
            index++;

        return index;
    }
4
  • Clearly this allows your data file to overrun your array size if it contains more triplets than your array has elements. Commented Apr 13, 2012 at 18:55
  • When fopen() fails, besides printing a message you also want it to return , and stop executing the rest of your code. Commented Apr 13, 2012 at 18:56
  • @Amardeep , for testing purposes, i make my array very large and my data set in my file very small Commented Apr 13, 2012 at 18:57
  • @karlphillip Right, but the error is still here after changed Commented Apr 13, 2012 at 18:59

2 Answers 2

2

you repeatedly open filename filelocation but never close the file handle f. You would keep reading the first line over and over again and eventually run out of filehandles.

You can change filldata to take the file pointer check the snippet below i have added some additional checks , you also need to check the size of Data_point *a is within the allocated range as you fill it up

int filldata(struct Data_point *a, File *f) 


    if( fscanf(f, "%ld%lf%d", &(a->sampleNumber), &(a->value), &(a->status)) == 3)
        return 1;   
    else
        return 0;
}

int fillAll(struct Data_point *a, const int data_point_size,const char *filelocation)// I will pass the struct array and the location of my file string
{

    FILE *f;
    if((f=fopen(filelocation,"r"))==NULL) {
        printf("You cannot open");
       return 0;
    }


    int index=0;
    while(index < data_point_size &&  filldata(&a[index]))  {
        index++;
    } 
    fclose(f);
    return (index != data_point_size);
 }
Sign up to request clarification or add additional context in comments.

3 Comments

Right, i realized that few moments ago. So how do i read first line and then second and then third......?
You have the f duplicated in the first function isn't it?
and i think the filldata function now takes 2 parameter, so i need to pass the file descriptor to the function and no need to open a new FILE f inside the filldata function. right?
0

You are getting segmentation fault because of your while loop. It will never stop until the filldata returns 0. Before that happens, your program would have already crossed the array bounds when passing the &a[index]. Also, i believe that there is no guarantee that filldata would return a 0 as when that happens as the program will first try to access that out of bound memory in fscanf() thus causing runtime error or taking a garbage value and considering it as a success.

Correct me if i am wrong.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.