I want to execute a read function from a "material" vector (I created this type with "struct"). The reading will be executed from a file.
std::vector<material> material_read(std::string s)
{
std::ifstream data(s);
dados.exceptions(std::ios::failbit |
std::ios::badbit);
std::vector<material> infomaterials;
material mat_read;
while (data >> mat_read.idmaterial) {
data >> mat_read.idmaterial;
data >> mat_read.rho;
data >> mat_read.pricekg;
infomaterials.push_back(mat_read);
}
return infomaterials;
}
The file I should read (and I can not make changes) is as follows, with spaces after all lines:
1 1.02 82.76
2 2.81 29.45
3 1.46 14.41
4 1.15 31.54
5 1.04 71.10
6 1.11 87.05
7 2.84 13.81
8 1.56 27.55
9 2.63 71.30
10 0.87 25.59
11 2.99 24.83
I think reading each number on each line is going well. However, there is an error in the execution, when it reaches the end of the file:
terminate called after throwing an instance of 'std :: ios_base :: failure [abi: cxx11]' what (): basic_ios :: clear: iostream error Aborted (recorded core image)
I tested by printing something on the screen before and after each read operation. It really does all 33 readings I expected. But after reading the 33rd value, it does not even enter the while loop again, and then it shows the error.
It is possible that, as the data > > mat_read.idmaterial failed, give me the error message? Is this happening because the file ends with spaces?