read binary data problems

Joined
Aug 17, 2006
Messages
3
Reaction score
0
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!
 
Joined
Jul 30, 2006
Messages
5
Reaction score
0
As you write down a matrix in a file?
IMHO the mistake occurs at write - data smoothing prevents.

For an example:
If write in a file next structure
Code:
struct MyStruct {
	 byte FirstElement, // real size 1 byte
	 long SeecondElement // real size 4 bytes
}

The file will contain 8 bytes (4 bytes on each element)
 
Joined
Aug 17, 2006
Messages
3
Reaction score
0
So I am not certain how to implement the comment about MyStruct and I'm not certain I fully understand the comment. This could be my issue and lack of knwledge about struct.

I've been messing with the code and now I have the follwing:

Code:
  const int matrix_rows = 10;
  const int matrix_cols = 10;
  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);
    
  }

  for(int i=0; i<10; i++) {
    for(int j=0; j<10; j++) {
      printf("%2d ", pointer_lattice[i][j]);  
    }
    printf("\n");
  }

I get the following output:

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 0 0 0 6 0
0 0 12 0 0 0 18 0 0 0
1 0 0 0 7 0 0 0 13 0
0 0 19 0 0 0 25 0 0 0
8 0 0 0 14 0 0 0 20 0
0 0 21 0 0 0 2 0 0 0
15 0 0 0 16 0 0 0 22 0
0 0 3 0 0 0 9 0 0 0

This all of the data from the original matrix with some extra zeros. It seems to me in pointer_lattice I am reading each char (1 byte), but the data was written as 4 bytes. Some of this may have to do with the bainary fiel being saved as int. How do create a 2d array that just has the original matrix values?
 
Joined
Jul 30, 2006
Messages
5
Reaction score
0
pman said:
This all of the data from the original matrix with some extra zeros. It seems to me in pointer_lattice I am reading each char (1 byte), but the data was written as 4 bytes. Some of this may have to do with the bainary fiel being saved as int. How do create a 2d array that just has the original matrix values?

I now work with the first project on Visual C ++. (the my main languages others: Delphi, PHP...).
I can not precisely answer.
I think it is necessary to look options of the project.

I have solved this problem on the. First created the buffer. In it copied from different buffers and variables:

Code:
	 memcpy (dstBuff [12], srcBuf, 10);

But I had additional reasons. To act so.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top