0

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.

Justin
  • 165
  • 3
  • 14
  • @SouravGhosh: I don't think that [`while (!feof(fp))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) is an appropriate duplicate. Given that the data being read is strings, the `while (fscanf(...) == 3)` loop will properly detect EOF, and then the redundant (but not wholly incorrect) `while (!feof(fp))` loop will terminate too. We need to see the `add_to_contacts()` code, I believe. It might help to see the `read_cmd()` code, too; it isn't yet clear where `open_and_read()` is called from. – Jonathan Leffler Jun 02 '15 at 07:24
  • 2
    Justin: Please read up on how to create an MCVE ([How to create a Minimal, Complete, and Verifiable Example?](http://stackoverflow.com/help/mcve)) or SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)) — two names and links for the same basic idea. Provide us with a minimal data file that demonstrates the problem. Show what diagnostic printing you've used to see what's going on. For example, after the `fscanf()` and before the `add_to_contacts()` call, you should be printing the data that was just read. – Jonathan Leffler Jun 02 '15 at 07:26
  • @JonathanLeffler I see sir. Thanks for correcting me. – Sourav Ghosh Jun 02 '15 at 07:29
  • 2
    Justin: also note that: `char cmd; while (cmd != 'Q' or 'q'){ ` has at least two problems. (1) `cmd` is not initialized; (2) the `or` requires `` and translates the condition to `while (cmd != 'Q' || 'q')` and that always evaluates to true because `'q'` is not zero, so it is true, so the second half of the condition is true. You probably meant to write `while (cmd != 'Q' && cmd != 'q')` (note `&&` rather than `||` or `or`!). – Jonathan Leffler Jun 02 '15 at 07:34
  • Just posted the 2 functions required. I will read up on the 2 links you posted now – Justin Jun 02 '15 at 07:35
  • OK. I think my previous comment has your problem solved. You've also got memory management issues because you don't allocate a new `struct person` each time you read a value. You don't close the file you opened, either. I'm not quite sure what you'd see if you tried to print the linked list, but it would either be an infinite loop with a single name (the last name read), or it would be a one-item loop (with the last name read). It's worth having a printing function for linked lists on hand to make the debugging easy. Incidentally, your `struct person` doesn't have a `next` member, either! – Jonathan Leffler Jun 02 '15 at 07:38
  • How can you use only `person *p` when `person` should be `struct person`? Is that a C99 thing? Or did you not put a `typedef` you have in the real code? – Eregrith Jun 02 '15 at 07:47
  • 2
    @Eregrith: Good question. Since the `struct person` shown doesn't have a `next` member, we know it's not the correct structure. Either a `typedef struct person person;` is missing or the code was being compiled with a C++ compiler despite the C tag. – Jonathan Leffler Jun 02 '15 at 07:50
  • 2
    @Eregrith there must be a `typedef` somewhere. Otherwise, it won't compile in C. – Sourav Ghosh Jun 02 '15 at 07:50
  • @JonathanLeffler Oh ok that's what I thought. And this could work in cpp hmm. Thanks – Eregrith Jun 02 '15 at 07:55
  • I'm not sure why my malloc isn't working properly. Because it is under the for loop shouldn't it allocate memory to a new node each time? I also left out the typedef and next pointer declaration in the example above but they are there – Justin Jun 02 '15 at 19:09

0 Answers0