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 ;
}