0

** 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"); 
                }
            }
Tfo Tfo
  • 3
  • 1
  • Please explain the nature of the problem you are having. – Scott Hunter Feb 22 '22 at 20:30
  • 1
    "not working" is never a good problem description. Please give the exact input, expected result and actual result. – kaylum Feb 22 '22 at 20:31
  • 2
    And read [**Why is “while ( !feof (file) )” always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Andrew Henle Feb 22 '22 at 20:32
  • `for (int j=0; i < 1; ++j)` What is this loop for? Why is it using `j` in some places and `i` in others? What does the magic `1` represent? – kaylum Feb 22 '22 at 20:33
  • The function is declared to return a `Team`, but it has no `return` statement. Why does it need a return type if it's filling in the `info` array? – Barmar Feb 22 '22 at 20:34
  • Yes, you're right it needs to return the number of teams(How many teams are there)? – Tfo Tfo Feb 22 '22 at 20:36

0 Answers0