Hello,
I have a bug in my program somewhere and I cannot understand why. The program merely prints data from a binary file. Here is the code:
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdint>
using namespace std;
int main(int argc, char *argv[])
{
if (argc!=2)
{
cout<<"Invalid argument count, please provide a file name."<<endl;
return 0;
}
fstream file(argv[1],ios_base::binary|ios_base::in);
vector<int32_t> data;
int32_t tmp;
char buf;
while (!file.eof())
{//complicated i/o to ignore endianness
file.read(&buf,1);
tmp=buf<<24;
file.read(&buf,1);
tmp|=buf<<16;
file.read(&buf,1);
tmp|=buf<<8;
file.read(&buf,1);
tmp|=buf;
data.push_back(tmp);
}
for (size_t i=0; i<data.size(); ++i)
cout<<hex<<data[i]<<endl;
return 0;
}
Most of the time it works fine. However, sometimes it gives the wrong output. For example, when my test file contained (as read by a hex editor):
6A0EFFFF
8A0C0002
5CC00000
00000007
8E0E0001
4A0E0000
100A0000
8E0EFFFF
6F0E0000
8F0E0000
4F0E0001
100A0000
The program spat out:
ffffffff
8a0c0002
ffc00000
7
8e0e0001
4a0e0000
100a0000
ffffffff
6f0e0000
8f0e0000
4f0e0001
100a0000
0
Why is this?