大家好,我對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小時+),試圖將代碼拼湊起來,讓我的工作。我會繼續研究和更新我所能做的。
我發現有多個線程關於存儲第一個數字與向量,但我需要使用數組。 –
爲什麼玩家陣列是一個整數?它似乎是一個玩家名字的字符串數組。 –
哎呀,是的,只是把它改成了字符串。 –