2013-06-20 114 views
0

大家好,我對C++並不陌生,但我的技能並沒有被磨練。無論如何,我有一項任務是我無法按時完成的,而且它讓我無法讓我的代碼正常工作。現在我只想完成它,以便我可以知道如何爲將來的任務。C++讀取.txt文件列到數組

數據文件包含玩家人數及其在同一行上的分數,最後一列是他們的時間。

問題是這樣的,有一個.txt文件,我必須在我的程序中打開,讀取爲(沒有項目符號)。

  • 彈出23 45 92 34 43 125
  • COP 4 23 56 23 75 323
  • ...等等。

如何存儲/忽略第一個變量,然後用數據列按列(分隔空白)創建數組?

這是我創建的。我創建了需要存儲各自數據的數組。

#include <iostream> 
#include <cmath> 
#include <ctime> 
#include <iomanip> 
#include <vector> 
#include <string> 
#include <sstream> 
#include <fstream> 
using namespace std; 

int main() 
{ 
    cout<<"Welcome to the Score Sorting Program! \n"; 
    cout<<"Please choose a number that corresponds with the way you would like to sort. \n"; 
    cout<<"You may also search by player if you know their username. \n"; 

    string players[50]; //Allocated space for 50 players. 
    int score1[27]; //Array for Score 1 
    int score2[27]; // 
    int score3[27]; // 
    int score4[27]; // 
    int score5[27]; // 
    int time[27]; //Array for Time 
    int i = 0; 
    int j = 0; 

    ifstream myfile; 
    myfile.open("G2347M.txt"); 
    if (!myfile) 
    { 
     cout<<"Could not open file. \n"; 
     system ("Pause"); 
     return -1; 
    } 

    while (!myfile.eof()) 
    { 
     getline(myfile, players[i], ' '); 
     if (i == 26) 
     { 
      i=0; 
      ++j; 
      getline(myfile, players[i],' '); 
     } 
     i++; 
    }  

} 

所以基本上我會將玩家與他們的分數對齊並輸出到另一個文件。 我只想讓這個閱讀文件的第一部分,然後我將繼續前進。

我研究過類似的話題(4小時+),試圖將代碼拼湊起來,讓我的工作。我會繼續研究和更新我所能做的。

+0

我發現有多個線程關於存儲第一個數字與向量,但我需要使用數組。 –

+1

爲什麼玩家陣列是一個整數?它似乎是一個玩家名字的字符串數組。 –

+0

哎呀,是的,只是把它改成了字符串。 –

回答

0

這是一個使用操作符>>的封裝和重載的方法。我假設你所描述的輸入只是一個由空格分隔的輸入值的長序列,第一個值是玩家的數量。

#include <iostream> 
#include <iomanip> 
#include <string> 
#include <sstream> 
#include <fstream> 
#include <ctype.h> 

using namespace std; 

static const size_t MaxNumPlayers = 50; 

// C++ is about encapsulation, and the player data is 
// discrete yet related, so encapsulate it. 
struct Player 
{ 
    string m_name; 
    int m_scores[5]; 
    size_t GetNumScores() const { return sizeof(m_scores)/sizeof(m_scores[0]); } 
    int m_time; 
}; 

// This allows us to write an operator that will read a player 
// directly from an ifstream. 
std::ifstream& operator>>(ifstream& istr, Player& player) 
{ 
    istr >> player.m_name; 
    for(size_t i = 0; i < player.GetNumScores(); ++i) 
    { 
     istr >> player.m_scores[i]; 
    } 
    istr >> player.m_time; 
    cout << "player: " << player.m_name << ", " << player.m_scores[0] << " @ " << player.m_time << endl; 
    return istr; 
} 

int main(int argc, const char** argv) 
{ 
    ifstream myfile("G2347M.txt"); 
    if (!myfile) 
    { 
     cout<<"Could not open file. \n"; 
     system("Pause"); 
     return -1; 
    } 

    // We can read the number of players directly. 
    size_t numPlayers = 0; 
    myfile >> numPlayers; 
    if (numPlayers <= 0 || numPlayers > MaxNumPlayers) { 
     cout << "Invalid number of players in score file ("<<numPlayers<<")." << endl; 
     system("Pause"); 
     return -1; 
    } 

    Player players[MaxNumPlayers]; 
    size_t playersRead = 0; 

    while (!myfile.eof()) 
    { 
     myfile >> players[playersRead++]; 
     if (playersRead >= numPlayers) 
      break; 
     // drain any spaces after the player. 
     while(isspace(myfile.peek())) 
      myfile.ignore(); 
    } 

    if (playersRead != numPlayers) 
    { 
     cout << "Problem reading score file, expected " << numPlayers << " but read " << playersRead << endl; 
     system("Pause"); 
     return -1; 
    } 

    cout<<"Welcome to the Score Sorting Program! I read "<<playersRead<<" players."<<endl; 
    cout<<"Please choose a number that corresponds with the way you would like to sort."<<endl; 
    cout<<"You may also search by player if you know their username.\n"<<endl; 

    /* ... */ 

    system("Pause"); 
    return 0; 
} 
+0

當您在頂部聲明MaxNumPlayers時,能否解釋size_t的用法?我對那裏正在發生的事情有點困惑,但是我發現它在主函數中再次使用,它讀取'if(numPlayers <= 0 || numPlayers> MaxNumPlayers){...}' – andrewec

+0

' size_t'是用於保存大小值的相對標準類型。 'static const size_t NumPlayers = 5;'是一個本地範圍的常量,換句話說,不是在每個地方都使用'5',我創建了一個有意義的命名值。如果你看到'int x = 32 + 5;'或'int x = 37;'你怎麼知道這些數字是什麼? 'int x = FirstPlayerNum + NumPlayers'自我解釋。 – kfsone

+0

這很有道理 - 但爲什麼不使用'static const NumPlayers = 5'?我想這就是我被掛斷的地方。 – andrewec

0

爲什麼50名球員只有27分?

我假設第一行是您期望讀取的行的總數?如果是這樣,你可以動態分配數組來保存所有的數據,而不僅僅是處理50行(或27行)。您也可以將一行的所有數據合併到一個結構中,而不是將它分散到一堆斷開的數組中。

僞代碼:

int lineNo = 1; 
int totalLines = -1; /* not set */ 

while(line = read_a_line()) { 
    if (lineNo == 1) { 
     totalLines = convert_line_text_to_number(); 
    } 
    else { 
     /* split the line into "tokens" separated by white space */ 
     /* store each token in the correct array */ 
    } 
    lineNo++; 
    /* if lineNo is too big, break the loop */ 
} 
+0

這是27行,但指示說爲最多50名玩家分配空間。在該文件中有27. –

0

使用它會用向量的最佳途徑。但正如你所說的,你必須使用數組,所以這裏是它的代碼。

#include <iostream> 
#include <fstream> 
#include <sstream> 

using namespace std; 

int main() 
{ 
    ifstream file; 
    file.open("sample.txt"); 

    string name[50]; 
    int score1[27],score2[27],score3[27],score4[27],score5[27],time[27]; 


    int i = 0; 
    while(!file.eof()) 
    { 
     string line; 
     getline(file,line); 
     char* a = const_cast<char*>(line.c_str()); 
     char* pch = strtok (a," "); 

     name[i] = pch; 
     int counter = 1; 
     while (counter < 7) 
     { 
      pch = strtok (NULL, " "); 

      if(counter == 1) 
      { 
       score1[i] = atoi(pch); 
      } 
      else if(counter == 2) 
      { 
       score2[i] = atoi(pch); 
      } 
      else if(counter == 3) 
      { 
       score3[i] = atoi(pch); 
      } 
      else if(counter == 4) 
      { 
       score4[i] = atoi(pch); 
      } 
      else if(counter == 5) 
      { 
       score5[i] = atoi(pch); 
      } 
      else if(counter == 6) 
      { 
       time[i] = atoi(pch); 
      } 

      ++counter; 
     } 

     ++i; 
    } 

    //cout<<a; 
    return 0; 
} 

如果您遇到任何問題,請讓我知道。我沒有添加一些基本的完整性檢查,例如文件檢查,請在您的版本中進行檢查。