Thread: Reading a binary file like a matrix of ints

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    52

    Reading a binary file like a matrix of ints

    Hi everyone,

    I am trying to make a simple program that will read
    a file like a matrix of ints.
    The idea is that there is a binary file with ints:
    1 2 3 4 5 6 7 8 9

    and I want to read them in a buffer 3 ints at a time, and then print
    them sepparated by spaces. Then I want to read then next 3 integers.
    However when I try the reinterpret cast on a buffer of 12 chars.. I
    get into problems. Any help appreciated.

    Ted


    I have this so far:

    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    
    const static int BUF_SIZE = 4;                    // 4 * num of ints
    using std::ios_base;
    
    int main(int argc, char** argv)
    {
        std::ifstream file("InvDat.dat",
            ios_base::in | ios_base::binary);
    
        char buf[BUF_SIZE];
    
        if(file.is_open())
        {
            while (file.read(&buf[0], BUF_SIZE))
            {
                for (uint32_t i = 0; i < 3; ++i)
                {
                    uint32_t *num = reinterpret_cast<uint32_t* >(&buf[0]);
                    std::cout << (uint32_t) *num << " ";
                }
                std::cout << std::endl;
            }
            file.close();
    
        }
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you want to read 3 ints at a time, then why not have a buffer

    int buff[3];

    Then you would only need to cast a pointer in file.read()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    52
    hmm you mean something like this:

    Code:
       int buf[BUF_SIZE];
    
        if(file.is_open())
        {
            while (file.read(reinterpret_cast<char*>(&buf[0]), BUF_SIZE))
            {
                for (uint32_t i = 0; i < 3; ++i)
                {
                    uint32_t *num = reinterpret_cast<uint32_t* >(&buf[0]);
                    std::cout << left << setw(width) << (uint32_t) *num << " ";
                }
                std::cout << std::endl;
            }
            file.close();
    
        }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by afflictedd2 View Post
    hmm you mean something like this:

    Code:
      
                for (uint32_t i = 0; i < 3; ++i)
                {
                    uint32_t *num = reinterpret_cast<uint32_t* >(&buf[0]);
                    std::cout << left << setw(width) << (uint32_t) *num << " ";
                }
    you are printing same number

    you need something like
    Code:
      
                uint32_t *num = reinterpret_cast<uint32_t* >(&buf[0]);
                for (uint32_t i = 0; i < 3; ++i)
                {
                    std::cout << left << setw(width) << num[i] << " ";
                }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Reading from binary file; fread problems?
    By pmgeahan in forum C Programming
    Replies: 3
    Last Post: 01-15-2009, 05:07 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 02:00 PM