I'm new to programming, looking for help in c.
I've got a simple structure:
struct list {
char name[10];
char sname[15];
int year, month, day;
};
And a text file:
john smith 1980 01 10
gabriel anderson 1975 11 15
john anderson 1991 03 11
Could you tell me how to read this file to an array of lists?
I don't cover much in how to read this file and extract the info into your structure! but i wanna look at your structure and give some comments instead.
I would suggest that you write down something like this
-
#define MAX_NAME_LENGTH (10)
-
#define MAX_SURNAME_LENGTH (15)
-
and use it througout your source code!
In fact, there is no difference between this one and your code but is more convenient. Why ?
Imagin you code a hundred of files with this structure. Now you believe that 10 is a maximum length of name ( or it is a requirement ). and 15 is the maximum of surename length. But what happen when there is a change in requirement, or you just get a new idea to update 10 to whatever, and 15 to whatever else.
So what you gonna do is to go through the hundred files and change it one by one. I would believe that you will spend a whole week to look for it and change it. So do you think you spend much longer just to do a little thing.
I would suggest you to use #define because we don't care how often you use MAX_NAME_LENGTH or MAX_SURNAME_LENGTH, or whatever you change 10 and 15. We only update in one place. So it is easy than before right?
Well there is another point i have to speak out in your structure, but wait tomorrow!.