Read a binary file

Discussion in 'C++' started by maxwell, Mar 12, 2005.

  1. maxwell

    maxwell Guest

    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
     
    maxwell, Mar 12, 2005
    #1
    1. Advertising

  2. maxwell

    Rolf Magnus Guest

    maxwell wrote:

    > 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.
     
    Rolf Magnus, Mar 12, 2005
    #2
    1. Advertising

  3. maxwell wrote:
    > 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
     
    Thomas Matthews, Mar 13, 2005
    #3
  4. maxwell

    Rolf Magnus Guest

    Thomas Matthews wrote:

    > maxwell wrote:
    >> 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?
     
    Rolf Magnus, Mar 14, 2005
    #4
  5. Rolf Magnus schrieb:
    > Thomas Matthews wrote:
    >>int main(void)
    >>{
    >> const unsigned int Matrix_Rows = 256;
    >> const unsigned int Matrix_Cols = 256;

    >
    >
    > Why unsigned?


    Why not?
     
    Michael Etscheid, Mar 14, 2005
    #5
  6. Rolf Magnus wrote:

    > Thomas Matthews wrote:
    >
    >
    >>maxwell wrote:
    >>
    >>>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
     
    Thomas Matthews, Mar 14, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug
    Replies:
    3
    Views:
    4,375
    Bruce Barker
    Nov 4, 2005
  2. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    934
    James Kanze
    Apr 28, 2008
  3. Jim
    Replies:
    6
    Views:
    735
  4. Sniper Abandon
    Replies:
    3
    Views:
    237
    Robert Klemme
    Nov 19, 2009
  5. Alex Dowad
    Replies:
    4
    Views:
    272
    Michel Demazure
    May 1, 2010
Loading...

Share This Page