从.txt文件读入struct数组

时间:2019-12-01 12:20:05

标签: c

我正试图从.txt文件中读取信息并将信息存储在struct数组中,由于无法获得所需的信息,我不确定如何进行此操作。下面是.txt文件的代码段:

Fri     2/12 6.00     AUS - USA      1 - 1    29534  
Fri     4/09 8.15     SWE - UK     3 - 1     35723  
Sat     11/05 7.00     ES - POR     1 - 2     48392

我想阅读日期,日期,时间,球队比赛,得分和观众人数。

这是我当前拥有的代码段:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 256


typedef struct {
    char dayPlayed[3];
    int datePlayed;
    double timePlayed;
    char teamPlayed;
    int score;
    int specs;
} matches[MAX_ARRAY_SIZE];

matches matches_array;

int main() {

    FILE *matchFP;
    matchFP = fopen("matches.txt", "r");

    while(fgets(matches_array, 60, matchFP)) {
        int i = 0;
        printf("%s", matches_array);
        fscanf(matchFP, "%s, %d, %lf, %s, %d, %d", 
                                    &matches_array[i].day[i], 
                                    &matches_array[i].day,
                                    &matches_array[i].time,
                                    &matches_array[i].teams,
                                    &matches_array[i].score,
                                    &matches_array[i].specs);
        i++;
    }

    printf("\n\n%d", matches_array[2]);

    fclose(matchFP);
    return 0;
}

当我尝试打印matchs_array [2]来检查它是否被复制时,我只是在终端中得到一个随机数。

我该如何复制到结构数组中?

0 个答案:

没有答案