我有一个浮点数的向量,我试图找出向量中每个包含72个浮点数的段的总数。我用从文本文件中读取的浮点数填充向量.I我试图从文本文件和向量的大小找到它,它们是否相同?。他们会给出正确的答案。以下是我使用的方式 这种方式给我一个大于实际值的值,有人可以帮忙吗?
long fileSizeVector;
long fileSize;
long fileSizes;
vector<float> ReplayBufferVector;
ifstream in;
in.open("fileName.txt");
if(in.is_open())
{
in.setf(ios::fixed);
in.precision(3);
in.seekg(0,ios::end);
fileSizes = in.tellg();
fileSize = fileSizes/((72)*sizeof(float));
in.seekg(0,ios::beg);
while(!in.eof())
{
for(float f;in>>f;)
ReplayBufferVector.push_back(f);
}
fileSizeVector = ReplayBufferVector.size()/72*sizeof(float);
in.close();
}
答案 0 :(得分:0)
最简单的方法是计算花车的数量 保留正确的向量大小然后将所有浮点数读入向量..
std::ifstream file("fileName.txt";
std::size_t size = std::distance(std::istream_iterator<float>(file),
std::istream_iterator<float>());
file.close();
file.open("fileName.txt");
std::vector<float> data(size); // allocate enough space
// to hold all the floats you need.
// Normally I would use the iterators to
// construct the vector but because it is
// so large I want to reserve the space
// for it first.
// copy from the file to the vector.
std::copy(std::istream_iterator<float>(file),
std::istream_iterator<float>()
data.begin());
或者,如果您只想阅读文件:
std::ifstream file("fileName.txt");
std::vector<float> data(std::istream_iterator<float>(file),
std::istream_iterator<float>());