I'm making a database program that reads from a file info on tennis players. Everything works, but is there a simpler way of doing this? Is there anything I can improve here?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const int MAX_PLAYERS = 5;
struct player {
char *fullname[2];
int age;
double rating;
char *racketBrand;
}; typedef struct player tPlayer;
void readData(tPlayer *player){
FILE *file = fopen("database", "r");
if(!file){
perror("File not found");
return;
}
int idx = 0;
char buffer[50];
while(fgets(buffer, sizeof(buffer), file)){
player[idx].fullname[0] = malloc(20);
player[idx].fullname[1] = malloc(20);
player[idx].racketBrand = malloc(20);
sscanf(buffer, "%s %s %d %lf %s", player[idx].fullname[0], player[idx].fullname[1],
&player[idx].age, &player[idx].rating, player[idx].racketBrand);
idx++;
}
fclose(file);
}
void printInfo(tPlayer *player){
int idx;
for(idx = 0; idx < MAX_PLAYERS; idx++){
printf("***Name: %s %s ***Age: %d ***Rating: %.1lf ***Racket Brand: %s\n", player[idx].fullname[0],
player[idx].fullname[1], player[idx].age, player[idx].rating, player[idx].racketBrand);
}
}
int main(){
tPlayer player[MAX_PLAYERS];
readData(player);
printInfo(player);
}