You read from a file 4 bytes each when @mrgloom's original answer read 1 byte each. That's a huge difference.
mrgloom:
unsigned char temp=0;
file.read((char*)&temp,sizeof(temp));
You:
unsigned int temp=0;
fin.read((char*)&temp,sizeof(temp));
temp = flipBytes(temp);
fout << temp << ", ";
Perhaps you wanted to print the numerical value of pixels to the console? Then cast the byte value to (unsigned) int just before you print it:
unsigned char temp=0;
fin.read((char*)&temp,sizeof(temp));
fout << unsigned(temp) << ", ";
Btw, just FYI, there are standard library functions for byte order swapping:
Update
Here is a working solution (essentially identical to @mrgloom's code)
#include <iostream>
#include <fstream>
using namespace std;
#include <arpa/inet.h>
#define ERROR() do { \
cout << "I/O error at " << __func__ << ": " << __LINE__ << \
" (offset " << file.tellg() << ")" << endl; \
return; \
} while (0)
static void read_mnist(const string &full_path)
{
ifstream file (full_path.c_str(), ios::binary);
if ( ! file)
ERROR();
int magic_number=0;
int number_of_images=0;
int n_rows=0;
int n_cols=0;
if ( ! file.read((char*)&magic_number,sizeof(magic_number)))
ERROR();
magic_number= ntohl(magic_number);
if ( ! file.read((char*)&number_of_images,sizeof(number_of_images)))
ERROR();
number_of_images= ntohl(number_of_images);
if ( ! file.read((char*)&n_rows,sizeof(n_rows)))
ERROR();
n_rows= ntohl(n_rows);
if ( ! file.read((char*)&n_cols,sizeof(n_cols)))
ERROR();
n_cols= ntohl(n_cols);
cout << "Mgc==" << magic_number << "; NImg==" << number_of_images << "; "
"NRow==" << n_rows << "; NCol==" << n_cols << endl;
for(int i=0;i<number_of_images;++i)
{
cout << '[' << endl;
for(int r=0;r<n_rows;++r)
{
cout << '\t' << '[' ;
for(int c=0;c<n_cols;++c)
{
unsigned char temp=0;
if ( ! file.read((char*)&temp,sizeof(temp)))
ERROR();
cout << unsigned(temp) << ' ' ;
}
cout << ']' << endl;
}
cout << ']' << endl;
}
}
int main(int argc, char *argv[]) {
cout << "Process [" << argv[1] << "]" << endl;
read_mnist(argv[1]);
}
Transcript:
$ wget http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
(snip)
$ gunzip t10k-images-idx3-ubyte.gz
$ ./mrgloom t10k-images-idx3-ubyte
Process [t10k-images-idx3-ubyte]
Mgc==2051; NImg==10000; NRow==28; NCol==28
[
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
[0 0 0 0 0 0 84 185 159 151 60 36 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
[0 0 0 0 0 0 222 254 254 254 254 241 198 198 198 198 198 198 198 198 170 52 0 0 0 0 0 0 ]
[0 0 0 0 0 0 67 114 72 114 163 227 254 225 254 254 254 250 229 254 254 140 0 0 0 0 0 0 ]
[0 0 0 0 0 0 0 0 0 0 0 17 66 14 67 67 67 59 21 236 254 106 0 0 0 0 0 0 ]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 83 253 209 18 0 0 0 0 0 0 ]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 22 233 255 83 0 0 0 0 0 0 0 ]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 129 254 238 44 0 0 0 0 0 0 0 ]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 59 249 254 62 0 0 0 0 0 0 0 0 ]
[0 0 0 0 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