ifstream gives unexpected output

So here's the class I'm having trouble with (I'm mainly talking about the loadlevel and access functions, but i've included the whole class just in case):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class level : public texture
{
	protected:
		uint32_t* bounds = NULL;
		int numberOfBoundBoxes = NULL;
		uint32_t& access(const int boundBox, const int element) { return bounds[element+(boundBox*4)]; }
	public:
		void loadlevel(const char* levelname)
		{
			stringstream temp;

			//Load level bounds file
			temp << "assets/levels/" << levelname << '/' << levelname << ".bin";
			ifstream input(temp.str().c_str(), ios::binary);
			if (!input)
			{
				cout << "Failed to load level \"" << levelname << "\"\n";
				return;
			}

			//Check that size is correct
			input.seekg(0, ios::end);
			if (input.tellg()%16)
			{
				cout << "Invalid bounds file for level \"" << levelname << "\"\n";
				numberOfBoundBoxes=0;
				return;
			}
			numberOfBoundBoxes = input.tellg()/16;
			input.seekg(0, ios::beg);

			//Copy it to array
			bounds = new uint32_t[numberOfBoundBoxes*16]{};
			for (int boundBox{}, element{}; boundBox<numberOfBoundBoxes; ++element, element==4 ? element=0, ++boundBox : NULL)
			{
				input >> access(boundBox, element);
				cout << bounds[element+(boundBox*4)] << std::endl;
			}

			//Load level texture
			temp = stringstream();
			temp << "assets/levels/" << levelname << '/' << levelname << ".png";
			assign(temp.str().c_str());
		}
		uint32_t* getlevel() { return bounds; }
		int getboundsize() { return numberOfBoundBoxes; }

		level() {}
		level(window& renderer, const char* levelname)
		{
			assign(renderer);
			loadlevel(levelname);
		}
		virtual ~level() { delete[] bounds; }
};

So basically, I'm trying to load a 16 byte bin file, which would be loaded into an array of 4 32bit unsigned integers. The first value should be 0, and the other values should be different than that (I'm too lazy to figure out their exact value); but I get unexpected results:
0
0
0
0

what am I doing wrong?
You can't use operator>>() to read from a binary stream. You need to use std::istream::read().
how would I extract an integer using that method though - it extracts individual chars
oh and without ios::binary I get the same results
FILE * f = fopen (..., "rb");
int arrayRead[4];
fread(arrayRead, sizeof(arrayRead), 1, f);
fclose(f);

Sometimes FILE * is the simplest and easiest way to do things....
Sometimes FILE * is the simplest and easiest way to do things....

I do disagree. The example usage in
http://en.cppreference.com/w/cpp/io/basic_istream/read
looks simple too.
You can do what ocreus suggested with std::istreams:
1
2
3
4
uint32_t i;
stream.read((char *)&i, sizeof(i));
if (stream.gcount() != sizeof(i))
    //We didn't get enough bytes. Maybe we hit EOF or something else went wrong. 
The problem with this is that attempting to read your file on a different platform may not work, due to for example different endianness.
You'll want to pick a consistent format:
1
2
3
4
5
6
7
unsigned char data[4];
stream.read((char *)data, sizeof(data));
if (stream.gcount() != sizeof(data))
    //...
uint32_t i = 0;
for (int j = 0; j < 4; j++)
    i |= (uint32_t)data[i] << (8 * i);
Topic archived. No new replies allowed.