C++没有从文本文件中将值读入数组
C++ Not reading in values into array from text file
我一直试图将文本文件中的值读入两个数组,但最终在names
和scores
数组中一无所获。这是我的:
const int size = 6;
int names[size] = { 0 };
int scores[size] = { 0 };
int name;
ifstream inputFile;
inputFile.open("input.txt"); //opens up textfile
inputFile >> name;
while (!inputFile.eof()){
inputFile >> names[x] >> scores[x];
cout << names[x] << scores[x];
x++;
}
input.txt
6
Alice 50
Bob 100
Cathryn 75
Don 90
Emily 80
Flora 60
George 95
name
正在获取值6,但names
和scores
没有获取任何值。有什么问题吗?
您的程序无法运行,因为您意外地将names
初始化为数组类型int
,而不是类型std::string
。这会破坏整个线路inputFile >> names[x] >> scores[x];
。
愚蠢的错误。只需制作一个名为names
的std::string
s的数据结构并在其中放入内容即可。
相关文章:
- 如何将内容数组写入文本文件?
- C++将文本文件中的数据读取到结构数组中
- 将值从二维数组输出到文本文件
- C++数据文件、数组和计算赋值
- 如何拆分文件中.txt字母并使用c ++使用数组进行扑克?
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 如何从txt文件中读取多个不同长度的数组?
- 保存/加载大量短数组到二进制文件
- 从二进制文件中读取整数数组
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- C++编程从外部文本文件定义数组大小
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 如何将文本文件的特定行读取到 int 类型的数组中C++?
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 将系数存储在头文件的数组中("does not name a type"错误)
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 从文件读取,并写入函数C++中的数组
- 如何从具有两列的.txt文件创建并行数组?
- 读取二维数组文件