大家好,我对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小时+),试图将代码拼凑起来,让我的工作。我会继续研究和更新我所能做的。
我发现有多个线程关于存储第一个数字与向量,但我需要使用数组。 –
为什么玩家阵列是一个整数?它似乎是一个玩家名字的字符串数组。 –
哎呀,是的,只是把它改成了字符串。 –