I have a file called Players.txt which contains
Del Piero|3|Italy|Juventus Ronaldo|0|Portugal|Real Madrit
I want to read each ward into a separated array, like array players to contain players[NUM_PLAYERS][NAME_LENGTH]={ Del Piero,Ronaldo}
and so on with the other arrays,
I know that it need to use a function called fgets and some string functions.
here is what i tried; My questions are : Is there any other approach to my problem, like to use some different programs, or string programs? And how to get the number of goals form this file and store them into the file
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <ctype.h>
#define NUM_PLAYERS 20
#define NAME_LENGTH 100
#define COUNTRY_NAME 20
int main (void)
{
FILE *Players;
char player_name [NUM_PLAYERS][NAME_LENGTH] = {0};
char team_name[NUM_PLAYERS][NAME_LENGTH] = {0};
char country_name[NUM_PLAYERS][COUNTRY_NAME] = {0};
char temp_buffer[NAME_LENGTH]={0};
int goals_scored[NUM_PLAYERS] = {0};
char *ptr1,*ptr2,*ptr3;
int i;
Players = fopen("Players.txt", "r");
if (Players == NULL)
{
printf("File not found.\n");
}
else
{
i=0;
while ((fgets(temp_buffer,sizeof(temp_buffer), Players) != NULL) && i < NUM_PLAYERS)
{
ptr1 = strchr(temp_buffer, '|');
strncpy(player_name[i], temp_buffer, ptr1 - temp_buffer);
printf("the name of the player is %s\n.", player_name[i]);
i ++;
}
}
fclose(Players);
return 0;
}