read binary data problems

Joined
Aug 17, 2006
Messages
3
Reaction score
0
read binary data problems - ignore its a duplicate post, see other post

Sorry, I'm not sure how I posted it twice and i can't seem to delete the post.


Hello all,
I have a matrix that is as follows:

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

This was created in matlab and I saved it as a .bin file. I am now trying to open that file in C++. I got the following code off of this forum.

Code:
const int matrix_rows = 5;
const int matrix_cols = 5;
char area_matrix[matrix_rows * matrix_cols];
char pointer_lattice[matrix_rows][matrix_cols];
ifstream data_file("magic5.bin", ios::binary);

/* Reading an area of bytes */
data_file.read(area_matrix, sizeof(area_matrix));

/* Rewinding the input file */
data_file.clear(); // just in case.
data_file.seekg(0);

/* Reading a two-dimensional array of bytes */
for(int i = 0; i < matrix_rows; ++i) {
  data_file.read(pointer_lattice[i], matrix_cols);
}

My issue now is how do I look that data. I simply (naively, incorrectly) wrote

Code:
for(int i=0; i<21; i++) {
    for(int j=0; j<21; j++) {
      printf("%1d ", pointer_lattice[i][j]);
    }
    printf("\n");
  }

The result is as follows:

17 0 0 0 23
0 0 0 4 0
0 0 10 0 0
0 11 0 0 0
24 0 0 0 5

So I am getting some of the data down the columns. I think the first code I have works, I am just having trouble with using the data. Does it have something to do with converting char to int? How do I put the data I just read into a matrix (of similar form as the the one at the top of this post) to use for computations? Any help would be appreciated.

Thanks!
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top