The problem is I have text file for example
2.7 5.0 6.1
1.5 7.2 4.8
3.7 8.9 7.0
and i need to convert this to two-dimensional float array
how i can read this data and convert it to array.
The problem is I have text file for example
2.7 5.0 6.1
1.5 7.2 4.8
3.7 8.9 7.0
and i need to convert this to two-dimensional float array
how i can read this data and convert it to array.
If the size is fixed, it's really easy
Just a nested loop.
If it's not a fixed size, the problem with bigger distraction will be allocating the 2d array.Code:#include <fstream> #include <iostream> const int numRows=3; const int numCols=3; int main() { std::ifstream in("data.txt"); float tiles[numRows][numCols]; for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { in >> tiles[i][j]; std::cout << tiles[i][j] << ' '; } std::cout << '\n'; } return 0; }
Thanks but yes the size is not fixed