Read from file, Array of Structs.

This is a discussion on Read from file, Array of Structs. within the C Programming forums, part of the General Programming Boards category; Hello. Now i have a task of reading information from a file, i can do this and it works. i've ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    Unhappy Read from file, Array of Structs.

    Hello.
    Now i have a task of reading information from a file, i can do this and it works. i've done some of the assignment. My problem is starting to use array of structs more, well at least two at the same time and use this in a proper way.

    Right, Task 2 is to Read in the file with data, find a specific round, football/soccer, where there scored most goals, and print that round. there's 33 rounds. i've got it to print each round and amount of goals. like Round 1, 17 goals are scored, but that's succeeded through lots of if statements, which isn't intended.

    I'll post some of my code, without the if statements ofc.

    Code:
    /*start of files with structs*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define STRING_LENGTH 100
    #define ROUND_LENGTH 6
    
    typedef struct {
    int round_match, day, month, year, hour, min, goal_side1, goal_side2;
    char team1[STRING_LENGTH];
    char team2[STRING_LENGTH];
    char supporters[STRING_LENGTH];
    } matches;
    
    typedef struct{
    int round_round, goal1, goal2, day1, month1, year1, hour1, min1;
    char team3[STRING_LENGTH];
    char team4[STRING_LENGTH];
    char supporters1[STRING_LENGTH];
    }    round;
    
    typedef struct{
    char hometeam[STRING_LENGTH];
    char awayteam[STRING_LENGTH];
    } teams;
      
    void goalroundplus();
    
    /* main function in here which calls the other function*/
    
    /* functions below */
    void goalroundplus(){
    FILE *fp;
    int x = 0;
    int i = 1;
    int goalsround[33];/* = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ,13 ,14, 15 ,16 ,17 ,18 ,19 ,20 ,21 ,22 ,23 ,24 ,25 ,26 ,27 ,28 ,29 ,30 ,31 ,32 ,33";*/
    int c = 1;
    int goals = 0;
    
    matches *info = calloc(199, sizeof(*info));
    /*round *roundarray = calloc(33, sizeof(*roundarray));*/
    
    
    fp = fopen("scorefile.txt", "r");
    
    while(fscanf(
    fp,
    "%d %d.%d.%d kl. %d.%d %s - %s %d - %d %s",
    &info[x].round_match,
    &info[x].day,
    &info[x].month,
    &info[x].year,
    &info[x].hour,
    &info[x].min,
    info[x].team1,
    info[x].team2,
    &info[x].goal_side1,
    &info[x].goal_side2,
    info[x].supporters
    ) != EOF){
    
    
    }
    
    fclose(fp);
    
    }
    It's just after i call my function in the while loop, where i'm supposed to have put information into a new array of structs i think, then compare it somehow, and print the round with most goals. I've tried but have been unsuccesfull, and my mind is really blowing up.
    inside the while loop, i have the if statements.

    I really hope anyone can help on how i could used array of structs together.
    Please ask if there's anything.

    Thank you.
    Net

    P.s det file is a scorefile.txt with input like this:
    round date time t1 t2 score supporters
    1 16.07.2011 kl. 17.00 OB - FCN 2 - 0 6.965
    1 17.07.2011 kl. 14.00 FCM - SIF 1 - 2 5.370
    1 17.07.2011 kl. 16.00 ACH - HBK 3 - 0 2.227
    1 17.07.2011 kl. 16.00 SDR - FCK 0 - 2 4.992
    1 17.07.2011 kl. 18.00 BIF - AAB 2 - 2 11.868
    1 18.07.2011 kl. 19.00 AGF - LBK 2 - 1 12.229
    2 23.07.2011 kl. 17.00 SIF - BIF 0 - 1 4.173
    2 23.07.2011 kl. 19.00 FCK - OB 2 - 2 14.774
    2 24.07.2011 kl. 14.00 FCN - ACH 1 - 1 2.662
    2 24.07.2011 kl. 16.00 LBK - SDR 0 - 1 1.207
    2 24.07.2011 kl. 16.00 AAB - AGF 2 - 1 9.737
    2 25.07.2011 kl. 19.00 HBK - FCM 2 - 3 2.371

    t1 = team 1
    t2 = team 2

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Why do you need to read into a new array? What about the one you're already reading into (info)?

    Seems to me all you need in your loop body is to track if the current read line from the file indicates more goals than what you've read before. To do this you'd need some additional variable(s) to track what the current highest; you probably want to track the array index of the current highest goal total number (initialized to 0). Each time in the loop body you compare what you've just read with the total goals at the current saved max index value and update the index if appropriate. At the end of reading the file you've got the index value of the info array that has the highest total.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read colums from a file to inser them in structs
    By IMAC in forum C Programming
    Replies: 1
    Last Post: 03-28-2010, 02:48 PM
  2. Storing Info From file into Array of structs
    By bigparker in forum C Programming
    Replies: 16
    Last Post: 06-27-2009, 02:21 AM
  3. Read a file (binary) into an array of structs
    By Separ in forum C Programming
    Replies: 3
    Last Post: 04-14-2009, 09:09 PM
  4. Newbie: An Array of Structs from a .txt file?
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2008, 11:23 PM
  5. Saving array of structs to file
    By |deep| in forum C++ Programming
    Replies: 2
    Last Post: 06-01-2002, 08:14 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21