I have a file that looks like this name1 year1 director1 name1 year1 director1 ... I'd like to read it in, and store each to a member of a linked list. I've already opened the file what should I use? fscanf, fgets? I've tried fscanf, but when I asked to print out the member, I just get (null) here is my code tail=(movie *)malloc(sizeof(movie)); fscanf(fptr, "%s", tail->title); printf("%s", tail->title); Am I doing something wrong, or is this an error of fscanf?
Edit, that second line should be name2 year2 director2, and so on and so forth...
Your scanf will return a single string into the title field of the ( I assume ) movie struct that you ( hopefully ) allocated with your malloc ( you didn't check whether malloc succeeded ). I think you want: fscanf( fptr, "%s %s %s", tail->title, tail->year, tail->director );
Yep. that seemed to work. I think it had to do with the way I declared my strings. I had it as char * title, but when it changed it to char[50] title, it worked fine,
Join our real-time social learning platform and learn together with your friends!