Question: How to read a text file into a struct (C programming)? I have a text file with a list of first and last names, such

How to read a text file into a struct (C programming)?

I have a text file with a list of first and last names, such as:

Jack Sparrow

Bilbo Baggins

Ramona Flowers

etc.

I also have a struct of arrays defined:

typedef struct {

char firstname [100];

char lastname [100];

} names;

I need to prompt the user to enter the file name (names.txt), which will open the file and read each column in the text file into the 2 arrays above. How would I do that? I've tried mutiple things, but haven't gotten it to work. Also, the text file is just a list of names that I created and will be using so the user won't be entering any names, just the file name.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!