我正在尝试将数据从文件复制到数组中,但我使用的方法不起作用
** 我使用一个 5 列的 2D 数组来存储玩家的姓名,并使用一个数组来存储他的号码(号码将被视为字符串)。我想将文件的数据复制到数组中,但是当它到达二维数组时遇到问题。这是我唯一遇到困难的功能。其余功能很简单。 **
typedef struct {
int id;
char team_name[25];
char player_name[5][10];
char player_number[5][2]; // the player's number will be concidered a string
}Team;
// File content:
/*
1234
Real Madrid
Courtois 1
Carvajal 2
Alaba 4
Marcelo 12
-------------------
5678
Paris Saint Germain
Hakimi 2
Ramos 4
Messi 30
Neymar 10
*/
Team copy_data_to_array (Team info[]) {
FILE* ptr;
char read_line[25];
ptr = fopen("team_info.txt", "r");
if (ptr!=NULL)
{
while (!(feof(ptr))) {
fscanf(ptr, "%d", &info[i].id);
char ch = getc(ptr);
fgets(info[i].team_name, 25, ptr);
for (int i = 0; i < 5; ++i) {
for (int j=0; i < 1; ++j) {
fscanf(ptr, "%s %s", info[i].player_name[i], info[i].player_number[i]);
}
}
fgets(read_line, 25, ptr); // to read the line from file
}
fclose(ptr);
}
else {
printf("File cannot be allocated...\n");
}
}
**
I'm using a 2D array with 5 columns to store player's name, and an array to store his number (number will be concidered a string). I want to copy data of file into an array, but I'm having a problem when it reaches the 2D array. This is the only function that I'm struggling with. The rest of functions are straight forward.
**
typedef struct {
int id;
char team_name[25];
char player_name[5][10];
char player_number[5][2]; // the player's number will be concidered a string
}Team;
// File content:
/*
1234
Real Madrid
Courtois 1
Carvajal 2
Alaba 4
Marcelo 12
-------------------
5678
Paris Saint Germain
Hakimi 2
Ramos 4
Messi 30
Neymar 10
*/
Team copy_data_to_array (Team info[]) {
FILE* ptr;
char read_line[25];
ptr = fopen("team_info.txt", "r");
if (ptr!=NULL)
{
while (!(feof(ptr))) {
fscanf(ptr, "%d", &info[i].id);
char ch = getc(ptr);
fgets(info[i].team_name, 25, ptr);
for (int i = 0; i < 5; ++i) {
for (int j=0; i < 1; ++j) {
fscanf(ptr, "%s %s", info[i].player_name[i], info[i].player_number[i]);
}
}
fgets(read_line, 25, ptr); // to read the line from file
}
fclose(ptr);
}
else {
printf("File cannot be allocated...\n");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论