How to read matrix elements one by one from file, C++
This might be an elementary question. I'm new to C++ and I want to
implement a huge random matrix which might not fit in the memory. So I
think maybe I should write it into a file and read in element by element
in a stream.
What I want to check is, if the element is smaller than a threshold. If
yes, push the index of the element (i,j) in a stl vector for storage, if
no, go to the next element.
Basically I need a couple of lines that write a matrix AJM into
matrix.txt, and then read its element as:
for(int i = 0; i < ROWS; i++){ for(int j = 0; j < COLS; j++){
currentElement = "read AJM[i][j] from file"; } }
So "read AJM[i][j] from file" is not clear to me, I guess it's done by
iterate the file pointer to some certain position given by i and j, and
specify how many bytes to read, right?
Could you show me the C++ code to write matrix.txt and read in element one
by one knowing its index, so that I avoid storing the whole matrix in the
memory?
Thank you so much!
No comments:
Post a Comment