我正在尝试从文件读入数组。我的名为 Players.txt 的文件包含:
Del Piero|3|Italy|Juventus|
Ronaldo|0|Portugal|Real Madrit
我用过fscanf
,但它不能正常工作,我没有做正确的转换。
谁能帮我阅读并将它们存储到数组中。像要包含的数组播放器名称{ Del Piero, Ronaldo}
#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 country_name[NUM_PLAYERS][COUNTRY_NAME] = {0};
char team_name[NUM_PLAYERS][NAME_LENGTH] = {0};
int goals_scored[NUM_PLAYERS] = {0};
int i;
Players = fopen("G:\\COP2220\\Project 5\\Players.txt", "r");
if (Players == NULL)
{
printf("File not found.\n");
}
else
{
while (fscanf(Players, " %[^|]s %[^|]d %[^|]s %[^|]s",player_name[i],&goals_scored[i],country_name[i],team_name[i]))
{
printf("The player %s, scored %d from %s plays in %s\n", player_name, goals_scored,country_name, team_name );
}
}
fclose(Players);
return 0;
}