如何从不同的行中获取存储在类中的变量?

时间:2014-09-29 13:49:26

标签: c++ class

如何从.txt文件中读取每一行的程序,并在不同的地方每行存储3个变量?我不明白如何在同一个班级存储不同的价值。一行工作正常,但我尝试的更多并不起作用。

class Team
{
    public:
    string name;
    string dificulty;
    string section;
};

void GetTeamInfo(Team& ko);

int main()
{   
    Team ko;
    GetTeamInfo(ko);
    cout << ko.name << " ";
    cout << ko.dificulty<< " ";
    cout << ko.section<< " "; 

    system("PAUSE");
}

void GetTeamInfo(Team& ko, int & i)
{
    ifstream fd;
    fd.open("Team.txt");
    if (fd.is_open())
    {
        for(int i = 0; i < 10 ; i ++)
        {
        fd >> ko.name;
        fd >> ko.dificulty;
        fd >> ko.section ;

        }

    }
    else
    {
        std::cout << "Mistake can't open file 'Team.txt'\n";
    }
}

3 个答案:

答案 0 :(得分:1)

试试这个:

void GetTeamInfo(vector<Team>& kos)
{
    ifstream fd;
    fd.open("Team.txt");
    if (fd.is_open())
    {
        while (!d.eof())
        {
            Team ko;
            fd >> ko.name;
            fd >> ko.dificulty;
            fd >> ko.section;
            kos.push_back(ko);
        }
    }
    ...
}

答案 1 :(得分:0)

我建议你使用std::vector,因为你有很多团队。

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

using namespace std;

class Team
{
    public:
    string name;
    string dificulty;
    string section;
};

void GetTeamInfo(vector<Team>& ko_v);

int main()
{
    vector<Team> ko; // a vector of Teams
    GetTeamInfo(ko); // read from file inside a vector

    // print every team
    for(unsigned int i = 0; i < ko.size(); ++i) {
      cout << ko[i].name << " ";
      cout << ko[i].dificulty<< " ";
      cout << ko[i].section<< " ";
      cout << "\n";
    }


    //system("PAUSE"); // don't use system()
    return 0; // return 0 should be placed at the end of main
}

void GetTeamInfo(vector<Team>& ko_v) // you had an extra parameter here, no need to
{
    ifstream fd;
    fd.open("Team.txt");

    if (fd.is_open()) // check if file is open
    {
        while (!fd.eof()) // while you have more to read
        {
            Team ko;            // create a Team
            fd >> ko.name;      // read data
            fd >> ko.dificulty;
            fd >> ko.section;
            ko_v.push_back(ko); // store that Team in the vector of teams
        }
    }
    else
    {
      cout << "File not opened!\n";
    }
}

为什么不使用数组?您当然可以使用数组,但由于这是C ++,因此鼓励使用std :: vector。此外,您不必担心要从文件中读取的Team的数量。如果您使用过数组,则应该知道Team的数量,或者动态分配内存。

Why not use system(pause); ?

乍一看,我正在使用数组修改示例。

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

using namespace std;

class Team
{
    public:
    string name;
    string dificulty;
    string section;
};

void GetTeamInfo(Team* ko_ar, const int N);

int main()
{
    const int N = 3; // number of teams in the file
    Team ko[N]; // a vector of Teams
    GetTeamInfo(ko, N); // read from file inside a vector

    // print every team
    for(int i = 0; i < N; ++i) {
      cout << ko[i].name << " ";
      cout << ko[i].dificulty<< " ";
      cout << ko[i].section<< " ";
      cout << "\n";
    }


    //system("PAUSE"); // don't use system()
    return 0; // return 0 should be placed at the end of main
}

void GetTeamInfo(Team* ko_ar, const int N)
{
    ifstream fd;
    fd.open("Team.txt");

    int i = 0;
    if (fd.is_open()) // check if file is open
    {
        while (!fd.eof()) // while you have more to read
        {
            Team ko;            // create a Team
            fd >> ko.name;      // read data
            fd >> ko.dificulty;
            fd >> ko.section;
            if(i == N) {
              cout << "Read more than " << N << " teams\n";
              break;
            }
            ko_ar[i++] = ko; // store that Team in the vector of teams
        }
    }
    else
    {
      cout << "File not opened!\n";
    }
    cout << "Read " << i << " teams\n";
}

答案 2 :(得分:0)

使用向量,这里有一个完整的例子(注释):

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

using namespace std;

class Team
{
    public:
        // Adding a constructor.
        Team(string name, string dificulty, string section):
        name(name),
        dificulty(dificulty),
        section(section)
        {}

        string name;
        string dificulty;
        string section;
};

// Defining a convenience type;
typedef vector<Team> team_list_t;

// This function now receives a vector of teams.
void GetTeamInfo(team_list_t &tl);



int main()
{   
    team_list_t tl;
    GetTeamInfo(tl);

    for (vector<Team>::iterator it = tl.begin(); it != tl.end(); ++it)
        cout << it->name << " " << it->dificulty << " " << it->section << endl;

    // You can also ...
    for (int i = 0; i < tl.size(); i++)
        cout << tl[i].name << " " << tl[i].dificulty << " " << tl[i].section << endl;
}


void GetTeamInfo(team_list_t& tl)
{
    ifstream fd;
    fd.open("Team.txt");
    if (fd.is_open())
    {
        // Define variables;
        string name, dificulty, section; 

        // Read until EOF
        while(fd >> name >> dificulty >> section)
        {
            // Add teams to the vector/list.
            tl.push_back(Team(name, dificulty, section));
        }
    }
    else
    {
        std::cout << "Mistake can't open file 'Team.txt'\n";
    }
}