Read a binary file

M

maxwell

I used to program in matlab
An read an image file with :
fread(a,[256,256],0)
which gave me a matrix of 256*256 full of numbers which were the pidxel
values from 0 to 256.
How do i do the same in c++

i read with fin.read((char *) &pl sizeof pl)
which brings out the text in the file .
But the matrix of numbers just appears as nothing.

Could comeone please offer suggestions / tell me how to do it .
Max
 
R

Rolf Magnus

maxwell said:
I used to program in matlab
An read an image file with :
fread(a,[256,256],0)
which gave me a matrix of 256*256 full of numbers which were the pidxel
values from 0 to 256.
How do i do the same in c++

i read with fin.read((char *) &pl sizeof pl)
which brings out the text in the file .
But the matrix of numbers just appears as nothing.

What matrix? Show more code.
 
T

Thomas Matthews

maxwell said:
I used to program in matlab
An read an image file with :
fread(a,[256,256],0)
which gave me a matrix of 256*256 full of numbers which were the pidxel
values from 0 to 256.
How do i do the same in c++

i read with fin.read((char *) &pl sizeof pl)
which brings out the text in the file .
But the matrix of numbers just appears as nothing.

Could comeone please offer suggestions / tell me how to do it .
Max

#include <iostream>
#include <fstream>
using namespace std;

int main(void)
{
const unsigned int Matrix_Rows = 256;
const unsigned int Matrix_Cols = 256;
unsigned char area_matrix[Matrix_Rows * Matrix_Cols];
unsigned char pointers_matrix[Matrix_Rows][Matrix_Cols];
ifstream data_file("filename", 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 (unsigned int i = 0; i < Matrix_Rows; ++i)
{
data_file.read(pointers_matrix,
Matrix_Cols);
}

return 0;
}

The O.P. must check for errors and validate the
reading of data as column-wise or row-wise.

A matrix can be an area of contiguous bytes as
declared by "area_matrix" above, or as an array
of arrays of char, as declared by "pointers_matrix".
I don't trust the compilers to place all the
char arrays in pointers_matrix contigously.

The structure does allow for the arrays to be
located in different areas in memory:
pointers_matrix[0]
some variables
pointers_matrix[1]
other variables
pointers_matrix[2]
pointers_matrix[3]
more variables
pointers_matrix[4]
... etc ...

In summary, read data into a matrix depends on
the definition or structure of the matrix.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
R

Rolf Magnus

Thomas said:
maxwell said:
I used to program in matlab
An read an image file with :
fread(a,[256,256],0)
which gave me a matrix of 256*256 full of numbers which were the pidxel
values from 0 to 256.
How do i do the same in c++

i read with fin.read((char *) &pl sizeof pl)
which brings out the text in the file .
But the matrix of numbers just appears as nothing.

Could comeone please offer suggestions / tell me how to do it .
Max

#include <iostream>
#include <fstream>
using namespace std;

int main(void)
{
const unsigned int Matrix_Rows = 256;
const unsigned int Matrix_Cols = 256;

Why unsigned?
 
T

Thomas Matthews

Rolf said:
Thomas Matthews wrote:

maxwell said:
I used to program in matlab
An read an image file with :
fread(a,[256,256],0)
which gave me a matrix of 256*256 full of numbers which were the pidxel
values from 0 to 256.
How do i do the same in c++

i read with fin.read((char *) &pl sizeof pl)
which brings out the text in the file .
But the matrix of numbers just appears as nothing.

Could comeone please offer suggestions / tell me how to do it .
Max

#include <iostream>
#include <fstream>
using namespace std;

int main(void)
{
const unsigned int Matrix_Rows = 256;
const unsigned int Matrix_Cols = 256;


Why unsigned?
Because one can't have negative columns or rows in a matrix.

Although, I guess it all depends on where column or row zero
is logically placed.

Or perhaps it is because my fingers tend to type "unsigned"
a lot. ;-)

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top