从文件中读取浮点数并将其存储到数组中
Reading float numbers from a file and storing them into an array C++
我试图将一些浮点数从文件存储到数组中,以便进一步使用它们。到目前为止,我正在测试我的函数,看它是否存储了什么,但我在输出中得到的只是一个全是0的数组。我的文件有很多行,每行写200个数字。我的问题是如何加载所有的数字从文件到数组正确。谢谢!
下面是我的代码:#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
void LoadCoefs(const char* input, float output[], int size );
int main(int argc, const char * argv[])
{
float* coefs;
coefs = new float[200];
LoadCoefs("CoefsFile", coefs, 200);
for(int i=0; i<200;i++)
{
cout<< coefs[i] <<"n";
}
delete [] coefs;
return 0;
}
void LoadCoefs(const char* input, float output[], int size )
{
ifstream inp;
inp.open(input);
for(int i=0; i<size; i++)
{
inp >> output[i] ;
}
inp.close();
}
在我的电脑上这是有效的。你检查过你的信息流了吗?
ifstream inp(input),
if (!inp) {
std::cout << "Failed to open the file";
}
还有一件事,使用std::array或std::vector代替c风格的数组和指针。
我已经找到问题所在了。
1)我已经用绝对路径替换了文件的相对路径。
2)我的号码用逗号隔开。删除逗号后,我的数组从我的文件中加载了数字。
相关文章:
- 使用无符号字符数组有效存储内存
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 在c++中获取两个大int,并将它们存储在数组中
- 使用 Xcode 将双精度存储在数组C++中
- 访问表示 2D 矩阵的 1D 存储数组
- 从输入文件读取整数,仅存储数组C 中的唯一整数
- 如何在队列中存储数组
- 按列主要顺序迭代行大量存储数组
- 如何使存储数组的二进制搜索稳定
- 将行中的字段从textfile中更改为存储数组,而不是重新编写文件
- 存储数组索引值C++
- 存储数组的地方,如果我在返回后声明它
- 缺少存储数组地址的内存
- 使用static在函数中存储数组各个组件的值
- 如何存储数组的每个相邻子数组的和
- 在c++中存储数组映射
- 存储数组的地址列表,以解析原始的非终止文本
- 用于存储数组的字符串
- c++存储数组下一个位置的地址
- 在 for 循环中创建和存储数组