将.txt文件列读入数组
C++ Read .txt file columns into an array
大家好,我对c++并不陌生,但我的技能不是很熟练。无论如何,我有一个任务,我不能按时完成,它真的是困扰我,我不能让我的代码工作。现在我只是想完成它,这样我就可以知道如何为以后的作业。
数据文件包含玩家的数量和他们的分数在同一行,最后一列是他们的时间。
问题是这样的,我必须在我的程序中打开一个.txt文件,它的内容如下(没有项目符号)
- 27
- pop 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小时以上),试图将代码拼凑在一起以使我的工作。我将继续研究并尽可能地更新。
这是一种使用操作符>>封装和重载的方法。我假设你所描述的输入只是一长串空格分隔的输入值,第一个值是玩家的数量。
#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;
}
为什么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 */
}
使用它的最好方法是使用向量。但正如你所说,你必须使用数组,所以这里是它的代码。
#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;
}
如果你有任何问题请告诉我。我还没有添加一些基本的完整性检查,比如文件检查,请在你的版本中做。
相关文章:
- 如何拆分文件中.txt字母并使用c ++使用数组进行扑克?
- 如何从txt文件中读取多个不同长度的数组?
- 如何从具有两列的.txt文件创建并行数组?
- 如何像在 C++ 中处理数组一样对 .txt 文件中的字符进行寻址?
- 当我在 CLion 中读取数组中的 txt 文件时C++编码问题
- 如何将 txt 文件中的多个值输入到数组C++中
- Ascii .txt文件到字节数组 - C++
- 如何使用数组 (C++) 读取具有未定义数量的值的 .txt 文件?
- (C/C++)fscanf_s从txt文件以字符形式读取数组时缺少整数参数错误
- 从我的2d数组中的一列返回的字符串值被压缩为一个字符串(在Mac os上打开Windows txt文件)
- C++将 txt 数据和字符读取到 2D 数组中
- C++:从.txt文件中读取复数并存储到复数数组中
- C 从TXT文件读取数据并将其放入2D数组中
- 将数据从 TXT 文件存储到 strucutre 数组中
- 迭代 txt 文件中的对象数组
- 读取.txt文件并组织成二维数组
- 如何从.txt文件中读取多个2D数组,而无需每个之间的新线
- 将TXT文件读取到2D数组C
- 我在 36603 以上输入的任何数组大小时都会返回"堆栈溢出"错误。如何使字符串能够捕获整个.txt文件?
- C++将 txt 文件读取到 2D 数组中