将 txt 文件中两个单独值的数据引入数组

Bringing in data of two separate values from txt file into array

本文关键字:单独值 数据 数组 两个 文件 txt      更新时间:2023-10-16

所以我从文本文件中读取数据并将其放入数组中。我的代码工作正常并输出数据,除了输出数据,例如

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;
}