Thread: Reading from a .txt file into struct array

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    3

    Reading from a .txt file into struct array

    Hello,

    I'm trying to read lines from a .txt file and store them in a struct array. This is my code so far:

    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<string.h>
    
    #define MAX_SIZE 256
    
    
    typedefstruct{
        char dayPlayed[4];
        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");
    
        int i =0;
        while(fgets(matches_array,60, matchFP)){
            
            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);
        return0;}
    And this is my text file:

    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

    How can I accomplish this? I'm new to programming and would love help on this.
    Last edited by Alpha8; 12-01-2019 at 11:00 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    836
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    #define MAX_SIZE 256
     
    typedef struct {
        char weekday[4];
        int  month, day;
        int  hour, minutes;
        char away_team[4], home_team[4];
        int  away_score, home_score;
        int  specs;
    } Match;
     
    // Example line: Fri 2/12 6.00 AUS - USA 1-1 29534
    int read_matches(const char *filename, Match *matches) {
        FILE *fin = fopen("matches.txt", "r");
     
        int i = 0;
        while (fscanf(fin, "%s %d/%d %d.%d %s - %s %d-%d %d", 
                 matches[i].weekday,
                &matches[i].month,
                &matches[i].day,
                &matches[i].hour,
                &matches[i].minutes,
                 matches[i].away_team,
                 matches[i].home_team,
                &matches[i].away_score,
                &matches[i].home_score,
                &matches[i].specs) == 10)
            ++i;
     
        fclose(fin);
        return i;
    }
     
    void print_matches(Match *matches, int size) {
        for (int i = 0; i < size; ++i) {
            Match *m = &matches[i];
            printf("%s %2d/%2d %2d:%02d  %-3s - %-3s  %2d - %2d %6d\n",
                m->weekday, m->month, m->day,
                m->hour, m->minutes,
                m->away_team, m->home_team,
                m->away_score, m->home_score,
                m->specs);
        }
    }
     
    int main() {
        Match matches[MAX_SIZE];
        int size = read_matches("matches.txt", matches);
        print_matches(matches, size);
        return 0;
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    3
    Quote Originally Posted by john.c View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    #define MAX_SIZE 256
     
    typedef struct {
        char weekday[4];
        int  month, day;
        int  hour, minutes;
        char away_team[4], home_team[4];
        int  away_score, home_score;
        int  specs;
    } Match;
     
    // Example line: Fri 2/12 6.00 AUS - USA 1-1 29534
    int read_matches(const char *filename, Match *matches) {
        FILE *fin = fopen("matches.txt", "r");
     
        int i = 0;
        while (fscanf(fin, "%s %d/%d %d.%d %s - %s %d-%d %d", 
                 matches[i].weekday,
                &matches[i].month,
                &matches[i].day,
                &matches[i].hour,
                &matches[i].minutes,
                 matches[i].away_team,
                 matches[i].home_team,
                &matches[i].away_score,
                &matches[i].home_score,
                &matches[i].specs) == 10)
            ++i;
     
        fclose(fin);
        return i;
    }
     
    void print_matches(Match *matches, int size) {
        for (int i = 0; i < size; ++i) {
            Match *m = &matches[i];
            printf("%s %2d/%2d %2d:%02d  %-3s - %-3s  %2d - %2d %6d\n",
                m->weekday, m->month, m->day,
                m->hour, m->minutes,
                m->away_team, m->home_team,
                m->away_score, m->home_score,
                m->specs);
        }
    }
     
    int main() {
        Match matches[MAX_SIZE];
        int size = read_matches("matches.txt", matches);
        print_matches(matches, size);
        return 0;
    }
    When I run this code, nothing happens. Isn't it supposed to print?

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    836
    You didn't have to "quote" my entire post (or any of it) to simply ask a question.
    It probably couldn't open the file. I had a mistake in that I opened "matches.txt" instead of filename in read_matches.
    Try this:
    Code:
    int read_matches(const char *filename, Match *matches) {
        FILE *fin = fopen(filename, "r");
        if (!fin) {
            printf("Can't open file: %s\n", filename);
            return 0;
        }
        int i = 0;
        while (fscanf(fin, "%s %d/%d %d.%d %s - %s %d-%d %d", 
                 matches[i].weekday,
                &matches[i].month,
                &matches[i].day,
                &matches[i].hour,
                &matches[i].minutes,
                 matches[i].away_team,
                 matches[i].home_team,
                &matches[i].away_score,
                &matches[i].home_score,
                &matches[i].specs) == 10)
            ++i;
    
    
        fclose(fin);
        return i;
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    3
    Quote Originally Posted by john.c View Post
    You didn't have to "quote" my entire post (or any of it) to simply ask a question.
    It probably couldn't open the file. I had a mistake in that I opened "matches.txt" instead of filename in read_matches.
    Try this:
    My bad, and it still doesn't run. Thanks for helping though!

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    836
    Well it runs for me just fine, so it's probably just not opening your file. Alternatively, maybe your file doesn't have the exact structure you've presented.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with reading a file into array of struct
    By Lozy in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2014, 08:29 AM
  2. Replies: 2
    Last Post: 05-04-2013, 04:29 PM
  3. reading the content of a file into an array of struct
    By m_programmer in forum C Programming
    Replies: 6
    Last Post: 09-20-2012, 07:29 AM
  4. reading struct from file
    By seandil666 in forum C Programming
    Replies: 6
    Last Post: 12-07-2009, 10:32 PM
  5. reading into Struct from file
    By bcianfrocca in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2005, 07:38 PM

Tags for this Thread