hi there i am trying to build a linked list from read a file operation. this code only adds the first node to the list. the problem is i am facing with "error: request for member 'head' in something not a structure or union" error. here is the code;
it gives the errors in add_Node function in all lines which include
i try to add the first node only. so if i can manage to solve this problem i can continue on my work : ).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "PR-ADT.h"
void add_Node(LIST** , NODE*);
LIST* create_list(void);
int main( void ) {
LIST* list;
NODE* student;
list = create_list();
FILE* myfile;
char file_name[50];
char sentence[200];
printf("Enter the name of the file you want to create your list: ");
gets(file_name);
printf("\n");
myfile = fopen(file_name,"r");
while ( myfile == NULL ) {
printf("File cannot be opened!!! Enter a new filename: ");
gets(file_name);
printf("\n");
myfile = fopen(file_name,"r" );
}
while ( !feof(myfile) ) {
fgets(sentence,200,myfile );
puts(sentence);
}
rewind(myfile);
student = (NODE*) malloc(sizeof(NODE));
while ( !feof(myfile) ) {
fscanf(myfile,"%s", &(student->first_name) );
fscanf(myfile,"%s", &(student->last_name) );
fscanf(myfile,"%s", &(student->email_addr) );
fscanf(myfile,"%d", &(student->phone_num) );
fscanf(myfile,"%s", &(student->address) );
fscanf(myfile,"%s", &(student->city) );
fscanf(myfile,"%d", &(student->zipcode) );
}
add_Node(&list,student);
printf("%s", list->head->first_name );
return 0;
}
void add_Node(LIST** list, NODE* student) {
NODE* new_stu = student;
new_stu = (NODE*) malloc(sizeof(NODE));
if( !new_stu ) {
printf("ERROR NOT ENOUGH MEMORY!!!\n");
return;
}
else {
new_stu->fn_next = NULL;
if( *(list)->head == NULL ) {
new_stu->fn_next = *(list)->head;
*(list)->head = new_stu;
printf("Adding operation is succesfull\n");
*(list)->count++;
return;
}
}
}
Code:
*(list)
Comment