c - How to malloc linked list node while reading file -
i've posted question similar this, able further narrow down issue. i'm pretty sure know problem is, i've been stuck figuring out how solve it.
//assuming typedef struct person person; struct person{ char first_n[100]; char last_n[100]; char middle_n[100]; struct person* next; }; void open_file_and_read(char* file){ file* fp = fopen(file_name, "r"); if (fp != null){ while (!feof(fp)){ person* person = malloc(sizeof(person)); person->next = null; while (fscanf(fp, "%s %s %s", person->first_n, person->last_n, person->middle_n) == 3){ add_to_contacts(person); } } } } void open_write_file(char* file){ file* filep = fopen(file, "w"); person* copy; (copy = front; copy != null; copy=copy->next){ fprintf(filep, "%s %s %s\n", copy->last_n, copy->first_n, copy->middle_n); } fclose(filep);
}
void add_to_contacts(person* person){ printf("last_name: %s", person->last_name); if (head == null){ person->next = head; head = person; else{ person->next = head; head = person; } } int main(int argc, char * argv[]) { char* inputfilename = argv[1]; if (inputfilename == null){ inputfilename = "myrolodex"; } open_read_file(inputfilename); char command; while (command != 'q' && command != 'q'){ printf("starting read command\n"); command = read_command(inputfilename); evaluate_command(command); } open_write_file(inputfilename); clear_rolodex();
the printf prints out in file 1 node. ex: file contains:
bob lee steve
mike steven noel first, last, middle
james nguyen lee
the printf prints out leestevennguyen
. these should separate.
i believe problem has malloc being called once, when need called each time scanf. can't move person* person = malloc(sizeof(person));
under while...fscanf
loop because fscanf dependent on having person
created. how go mallocing each new person. similar assignment due in couple of hours , i've spent last 10 hours trying work , believe big part.
thank you
edit more detail: have print statement in add_to_contacts
function prints out node->lasts_name. added print statement before sorting function(not posted) , prints infinitely
there several ways resolve this.
the 1 think directly matches want is:
person* person = malloc(sizeof(person)); person->next = null; while (fscanf(fp, "%s %s %s", person->first_n, person->last_n, person->middle_n) == 3){ add_to_contacts(person); person = malloc(sizeof(person)); person->next = null; } free(person);
this assumes add_to_contacts
inserts malloc'd pointer data structure, need next fscanf
put data in different location.
having done this, there 1 person*
allocated @ end not ever passed add_to_contacts
, can , should free pointer when loop exits.
a slight variation is
person* person = malloc(sizeof(person)); while (fscanf(fp, "%s %s %s", person->first_n, person->last_n, person->middle_n) == 3){ person->next = null; add_to_contacts(person); person = malloc(sizeof(person)); } free(person);
(the idea there thing needs person->next = null;
here presumably add_to_contacts(person)
, , postponing person->next = null;
until last moment don't have call outside loop inside loop.)
an alternative (quite different) approach declare local variables, let fscanf
write them, , copy them newly allocated person
inside loop.
a third approach make person
stack-allocated (not malloc'd) variable, , make add_to_contacts
make own malloc'd copy of in order insert copy in data structure.
but these last 2 options involve fscanf
writing data 1 place , having copy in order put in larger structure. seems may want avoid copy.
Comments
Post a Comment