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