-1

I have a project of a phone book , I searched for hours for a good way to read data from file to a structure,

after a lot of trials i have a segmentation fault on this condition, I'm sure that I've a misunderstanding for this part :

        if(queue->head == NULL )

the entire code -so far- is below

 struct pb
{
    char Firstname[25];
    char Lastname[25];
    char Address[70];
    char email[50];
    char number[11];
    struct pb *next;


};


struct queue
{
    struct pb *head;
    struct pb *tail;
    int size;
};

void read_str(struct queue *queue){
{

    FILE *read ;

    char filename[40];

    printf("Enter file name \n");
    scanf("%s",&filename);
    read=fopen(filename,"r");

    if (read == NULL)
        printf("Error");
    else
         while(!feof(read))
            {
                struct pb *n= malloc(sizeof(struct pb));
                fscanf(read,"%s %s %s %s %s", &n->Firstname, &n->Lastname, &n->Address, &n->email, &n->number);
                n->next=NULL;

                if(queue->head == NULL )
                {
                    queue->head=n;
                }
                else
                {
                    queue->tail->next=n;
                }

                queue->tail=n;
                queue->size++;

            }
}
}



int main(){
    struct queue *q;
 read_str(q);

return 0 ;
}
  • Unrelated to your crash (possibly) but first you should read [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) and [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Dec 25 '15 at 16:34
  • As for your problem, please show how you use the `read_str` function, and how you initialize the `queue` structure you pass to the function. – Some programmer dude Dec 25 '15 at 16:35
  • @JoachimPileborg edited to include the code – Khaled Awad Dec 25 '15 at 16:45
  • 1
    In `main` you declare `q`, a pointer, but you don't initialize it to anything, so its value is undefined. You then pass it to `read_str`, which serves no purpose since you're just passing a random value which cannot be meaningfully used for anything. But then you try to dereference it in `read_str`. The results are undefined, but a segmentation fault is likely. – Tom Karzes Dec 25 '15 at 16:49
  • @TomKarzes Thank you too , I get it clearly :) – Khaled Awad Dec 25 '15 at 17:06
  • Check the return code from `fscanf`. Also, see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Andrew Henle Dec 25 '15 at 17:41

1 Answers1

2

In the main function you have the pointer q but you don't actually make it point anywhere. That means that the variable is uninitialized and its value will be indeterminate, then when you dereference the pointer (in queue->head) you will have undefined behavior.

The simple solution is to declare the structure not as a pointer but as an instance of the structure, e.g.

struct queue q;

But that's not enough, because the contents of q will be uninitialized, so do e.g.

struct queue q = { NULL, NULL, 0 };

Then to get a pointer to this structure, so you can pass it to your function, then use the address-of operator & like

read_str(&q);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I get it clearly Thanks :) , but a question out of scope , if i want the same function to read from a CSV file , what shall i do ? – Khaled Awad Dec 25 '15 at 17:06