读取文件,存储在数组中,错误:'operator>>'不匹配
Reading file, storing in array, error: no match for 'operator>>'
我有一个文件weights01.txt,它在4x3矩阵中填充了浮点数,如下所示
1.1 2.123 3.4
4.5 5 6.5
7 8.1 9
1 2 3.1
我正在尝试读取这个文件并将数据传输到一个名为newarray的数组。这是我使用的代码:
int main()
{
ofstream myfile;
float newarray[4][3];
myfile.open ("weights01.txt");
for(int i = 0 ; i < 4; i++) // row loop
{
for(int j = 0 ; j < 3; j++) // column loop
{
myfile >> newarray[i][j]; // store data in matrix
}
}
myfile.close();
return 0;
}
我得到了一个错误的行
myfile >> newarray[i][j];
错误:没有匹配'operator>>'在'myfile>> newarray[i][j]'
我不明白为什么会出现这个错误
我搜索了以前关于这个"no match for 'operator>>"错误的问题,包括这个和这个。我还阅读了这篇关于重载操作符的长篇讨论,但我没有找到解释(可能是因为我以前没有经常使用文件,并且没有真正了解发生了什么)。
您不能从std::ofstream
(out文件流的缩写)中读取,它仅用于输出。使用std::ifstream
(即文件流中的)代替。
如果您对哪个标准库功能做什么有疑问,请查看您最喜欢的参考资料,例如cppr。
OT注释:您可以直接从文件名构造流:
std::ifstream myfile ("weights01.txt");
和你不需要close()
文件当你完成后,流的析构函数将为你处理
相关文章:
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- C4018:类内有符号、无符号不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- RE2 不匹配非 ASCII 字符
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- 引号之间匹配/不匹配,带有不可避免的引号和多行
- 映射迭代器与运算符不匹配
- C++调用和定义不匹配
- 模板专用化与派生类不匹配