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;
}