vendredi 21 octobre 2016

Reading from text file to structure

Firstly, I'd like to explain what I'm trying to do. So, I have a text file with 10 names followed by their points. I'm trying to create a structure and read information from a file using a function (void). And that's my code and text file:

code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void read(struct laLiga t[]) {
    ifstream infile("info.txt");
    int i = 0;
    while(infile >> t.team[i] >> t.points[i]) {
        infile.get(t.team);
        infile >> t.points;
        i++;
    }
}

struct laLiga {
    char team[50];
    int points;
};

int main() {
    struct laLiga t[10];
    read(t);
    return 0;
}

text file:

Athletic Bilbao  15
Atletico Madrid  18
Barcelona  16
Alaves  10
Las Palmas  12
Real Madrid  18
Real Sociedad  10
Sevilla  17
Eibar  11
Villarreal  16

Aucun commentaire:

Enregistrer un commentaire