如何将字符从 txt 文件读取到 2D 数组C++
How to read chars from a txt file to a 2d array C++
所以我有一个包含球队和分数的文件,我需要将球队读入 2D 字符数组,然后将他们的分数读取到整数数组中。
这是我函数的当前代码,如何在团队名称完成后停止读取,然后将分数存储在单独的数组中?
void getData (char array [][COLS], int rows, int scores [])
{
ifstream inFile; //Input file stream object
//Open the file
inFile.open ("scores.txt");
if (!inFile)
{
cout << "Error opening data file!n";
exit(102);
}
for (int r = 0; r < rows; r++)
{
{
//reads through columns
for (int c = 0; c < COLS; c++)
{
inFile >> array[r][c];
}
}
}
for (int count = 0; count < ROWS; count++)
{
for(int i = 0; i < COLS; i++)
{
cout << array[count][i];
}
}
inFile.close();
}
我的输入文件如下:
Jaquars 23
Colts 23
49ers 13
Lions 13
Titans 7
Redskins 38
Cardinals 14
Buccaneers 36
Seahawks 30
Lions 24
Bears 28
Packers 23
Bears 14
Rams 22
Texans 6
Packers 34
也许像这样
for (int r = 0; r < rows; r++)
{
int c = 0;
// keep reading until we hit a space
char ch = inFile.get();
while (ch != ' ')
{
array[r][c] = ch;
c++;
ch = inFile.get();
}
// now read the score
inFile >> scores[r];
// now read until we hit the trailing newline
ch = infile.get();
while (ch != 'n')
{
ch = inFile.get();
}
}
这应该让你知道如何去做。我还没有编译它。但可以很好地说明这一点。我强烈建议您选择基于地图的解决方案。或者使用矢量。
#include <string>
#include <iostream>
#include <fstream>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
ifstream in("DataFile.txt");
string line;
char * pch;
while(getline(in, line))
{
pch = strtok (line.c_str()," ");
while (pch != NULL)
{
//Put some logic here so that in first loop iteration
//first segment(name) gets stored in your char arrays.
//In second iteration, which will return the score, you store that
//in the int array. Do not forget to use atoi() to convert to int before storing.
pch = strtok (NULL," ");
}
}
//There are more elegant ways to split a string in C++
//http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c
return 1;
}
这是我
的代码void getData (char array [][COLS], int rows, int scores [])
{
ifstream inFile; //Input file stream object
//Open the file
inFile.open ("scores.txt");
if (!inFile)
{
cout << "Error opening data file!n";
exit(102);
}
for (int r = 0; r < rows; r++)
{
int c = 0;
char ch;
// keep reading until we hit a space
ch = inFile.get();
while (ch != ' ')
{
array[r][c] = ch;
c++;
ch = inFile.get();
}
// now read the score
inFile >> scores[r];
// now read until we hit the trailing newline
ch = inFile.get();
while (ch != 'n' && ch != EOF)
{
ch = inFile.get();
}
}
for (int count = 0; count < rows; count++)
{
for(int i = 0; i < COLS; i++)
{
cout << array[count][i];
for (int count2 = 0; count2 < rows; count2++)
cout << scores[count2];
}
}
inFile.close();
return;
}
相关文章:
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 读取字符串文本输入以创建 2D 矢量
- 如何从文件中读取值,然后使用它们来初始化 2D 数组C++
- 如何将逗号分隔的文件读取为 2D int 数组?
- 从文本文件中读取一行并将其存储在 C++ 中的 2D 矢量中
- 当我在将文件读取到C字符串的2D数组后尝试对任何内容进行定制时,C++分段错误
- 将文本文件读取到2D数组中,但数组开始为超过数组[39][]的任何内容输出胡言乱语
- C++2D网格数组,从文件中读取和插入数组值
- C++将 txt 数据和字符读取到 2D 数组中
- 从文本文件读取到 2D 数组中
- 未正确读取 2D 数组元素
- 从文本文件读取 2D 数组错误
- 尝试读取 2D 数组时"no matching function for call"
- 从二进制读取 2D 浮点数组,从 char* 转换为浮点数[2]
- 读取 2D 矢量
- 从控制台读取 2D 数组
- 从制表符分隔的文件中读取2D数据并存储在矢量C++中
- 在c++中使用boost::spirit读取2D数组
- 函数不读取 2D 数组的 [i][0] 行
- 读取2d数组并显示它(c++)