Quote Originally Posted by e8johan View Post
The easiest would be to store the dimensions to expect first, for example, this matrix:

1 1 1
2 2 2
3 3 3

Would be stored as

3 3 1 1 1 2 2 2 3 3 3

Where the first two threes would indicate that we're dealing with a 3x3 matrix. Then the data follow. Using an index, starting at 0 for the first one, then increasing you can find your matrix coordinate like this:

row = index/width; // Integer division, always rounds down
col = index%width; // Modulus

The final check would be to ensure that the index == width*height when you have reached the end of the file.

Also, when storing binary data using Qt, make sure to set your QDataStream version using myDataStreamObject->setVersion( xxx ); to ensure compatibility between Qt versions.
Hi e8johan,
I have written a sample program about what you have told in above post. Please correct me if I'm wrong...

Qt Code:
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int arr[3][3] = { 1, 1, 1,
  8. 2, 2, 2,
  9. 3, 3, 3 };
  10.  
  11.  
  12. ofstream ofs("test.txt", ios::binary);
  13. const char* data = "33111222333";
  14. string line = data;
  15. ofs.write( line.data(), line.length() );
  16. ofs.close();
  17.  
  18. char buffer[256];
  19. ifstream ifs("test.txt", ios::binary);
  20. ifs.getline(buffer, sizeof(buffer) );
  21. ifs.close();
  22.  
  23. string bufferdata = buffer;
  24.  
  25. int width = bufferdata.at(0) - '0';
  26. int height = bufferdata.at(1) - '0';
  27.  
  28. int index = 0;
  29. int row = 0, col = 0;
  30.  
  31. while(1)
  32. {
  33. if( index == width*height )
  34. break;
  35.  
  36. row = index/width; // Integer division, always rounds down
  37. col = index%width; // Modulus
  38.  
  39. cout<<"Row:"<<row<<" "<<"Col:"<<col<<endl;
  40. bufferdata.at(index++);
  41. }
  42. return 0;
  43. }
To copy to clipboard, switch view to plain text mode