2014-09-29 33 views
0

如何讓程序從.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"; 
    } 
} 
+1

嘗試使用數組。 – d3L 2014-09-29 13:51:03

+0

如何在這裏使用數組? – Blank 2014-09-29 14:37:02

+0

如果有3個以上的團隊會怎麼樣?像一次10人隊其他25人?隨機。 – Blank 2014-09-29 15:16:21

回答

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的數量。如果你想使用一個數組,你應該知道apriori的數量,或者動態分配內存。

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"; 
} 
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); 
     } 
    } 
    ... 
} 
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"; 
    } 
} 
相關問題