I have my struct:
struct person{
char first_n[100];
char last_n[100];
char middle_n[100];
};
I'm trying read in a file and then store the data into linked list nodes, then continue to perform certain actions depending on the char commands, but my program is infinitely looping asking for commands, and I've been stuck for hours. I added some print statements and noticed it is segfaulting at this function:
void open_and_read(char* file){
FILE* fp = fopen(file_name, 'r');
if (filep != NULL){
while (!feof(fp)){
person* p = malloc(sizeof(person));
p->next = NULL;
while(fscanf(fp, "%s %s %s\n", p->first_n, p->last_n, p->middle_n) == 3){
add_to_contacts(p); //calls function to add node to front of linked list
}
}
}
}
char read_cmd(char* file_name){
char cmd;
printf("%s Command:", file_name); //ex: C:/user/word.txt Command:
scanf("%c", &cmd);
return cmd;
}
void add_to_contacts(person* p){
if (head_pointer != NULL){
p->next = head;
head = p;
return;
}
p->next = head;
head = p;
}
char* file_name = argv[1]; //assuming main takes in arguments.
open_and_read(file_name);
char cmd;
while (cmd != 'Q' or 'q'){
cmd = read_cmd(file_name);//calls simple function that returns char
evaluate_cmd(cmd); //if else statements to determine which functions to call
}
EDIT: I've seen a similar question, and I believe I tried the solution there by using malloc every time I create a new node. However, my problem persists.