The code
char dim;
in.read((char*)&dim, sizeof(int));
defines a 1 byte char then reads sizeof(int)
bytes (which is likely to be greater that 1) into it. This is invalid and may corrupt your stack.
If you need to read sizeof(int)
bytes, declare dim
as int
. Otherwise, change the number of bytes you read to 1. It'd be best if you also used sizeof(dim)
to ensure that you only read as many bytes as you've provided storage for:
in.read((char*)&dim, sizeof(dim));