将 txt 文件中两个单独值的数据引入数组
Bringing in data of two separate values from txt file into array
所以我从文本文件中读取数据并将其放入数组中。我的代码工作正常并输出数据,除了输出数据,例如
Q 5
它像这样输出它
Q
5
这是更大配置的一部分,我将所有值放入队列中,并根据数值对它们进行排序,在上面的例子中,数值为 5。但我只是想要帮助,试图让数据像
Q 5
这是我的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int alphabet = 52;
char letter[alphabet];
int count = 0;
ifstream dataIn;
dataIn.open("CharInput.txt");
if (!dataIn)
{
cout << "Error opening data filen";
}
else
{
while (count < alphabet && dataIn >> letter[count])
count++;
dataIn.close();
cout << "The letters and their position are: " << endl;
for (int stuff = 0; stuff < count; stuff++)
{
cout << letter[stuff] << endl;
}
}
system("PAUSE");
return 0;
}
数据文件名为CharInput.txt并具有:
26
Q 5
W 3
E 8
R 7
T 2
Y 9
U 0
I 9
O 6
P 1
A 2
S 2
D 4
F 3
G 6
H 9
J 8
K 0
L 3
Z 1
X 5
C 7
V 4
B 7
N 2
M 8
数据以行为单位输出,因为您打印的数据以 std::endl
结尾,顾名思义,以当前行结尾。
相反,请在每个元素及其计数之间使用空格:
for (int stuff = 0; stuff < count; stuff += 2)
{
cout << letter[stuff] << " " << letter[stuff + 1] << std::endl;
}
相关文章:
- C++Lua用户数据扰乱了Mathfu浮点值
- C++数据文件、数组和计算赋值
- 如何实现高效的算法来计算大型数据集的多个不同值?
- QT QOpenGLWidget:如何在不使用数据块复制的情况下修改VBO中的单个顶点值?
- 从模板创建通用打印函数,以打印基元数据类型变量的值
- 是否可以使用右值初始化数据成员?
- C++数据成员:值与指针
- 使用 OpenGL 插值数据缓冲区?
- 将数据从文件读取到单独的int值
- 是否有一个更适合键值数据结构的QABSTRACTITEMMODEL类型类/接口
- 我应该使用哪些键值数据结构?按值快速检索和按键快速检索
- 将八叉树叶节点的顶点保存在单独的数据结构中
- 在单独的数据中显示数组数据
- 使用 cpp/c++ 中的字符串值数据声明变量名称
- 用于 Hadoop 的可移植键值数据文件格式
- 将 txt 文件中两个单独值的数据引入数组
- 如何使用rapidjson C++在JSON文件中检索键值数据
- 存储文件中的键值数据
- 用C++为继承的嵌套类提供一个单独的数据类型
- 如何使用 Windows API 确定注册表值数据的大小?