This is structured incorrectly, pun intended. fgets reads one line from a file :
fgets - C++ Reference[
^]. Car should not be a separate structure. It should be part of human since there is no more data for it other than the model. The bigger problem is how one line of text is read but four different variables are set from that single line of text. You need to have a counter and switch/case statement so you assign the appropriate variable for each line.
What that means is you read four lines of data for one entry. The first line is the name, then the age, etc. You have to count those lines as you read them. After four lines you have completed one entry so move on to the next one. Remember to skip empty lines and you might want to trim the new lines off the end too. If you do that first then an empty line is easy to catch. You will probably wonder how to trim the new line off. Well, start by figuring out how to determine what the last character of the line is. The function strlen is helpful for this.
Since you're giving it an honest try and not the "Oliver Twist" way we see so often, ("please sir, can I have some code?"), here's one way to trim the new lines off.
int TrimNewLines( char * text )
{
int index = (int) strlen( text );
--index; while( index >= 0 )
{
if( text[ index ] == '\r' )
text[ index ] = 0;
else if( text[ index ] == '\n' )
text[ index ] = 0;
else
break; --index; }
return index + 1; }
On the topic of strlen, you should learn how to use it instead of allocating 40-character strings. There is a handy function for that called strdup. You can make your own version of that if you want to - remember to allocate one more than the length of the string so you have room for the null.
One more thing - if you allocate 60 bytes for your buffer then you should not try to read up to 100 characters into it. Here again, remember the null. The way I always do this is to 'hide' it.
#define BUFFER_LENGTH = 99
char buffer[ BUFFER_LENGTH + 1 ] = { 0 };
while( fgets( buffer, BUFFER_LENGTH, fp ) )
{
}