I am trying to read and write some boolean grids to a file using stdio.h. The user inputs a number nx (from 1 to 10, generally) and the program generates a list of nx by ceil(nx / 2) boolean grids (ceil(nx / 2) is ny). The grids themselves are stored in __int64s, so this grid (f is false and T is true):
ffTT
fTfT
would be 172 (10101100).
The end list of grids is outputted to a binary file.
My code:
std::vector<__int64> grids;
...
FILE *cFile;
if (fopen_s(&cFile, ("grid_" + std::to_string(nx) + "_c.bin").c_str(), "wb") != 0) return;
for (int i = 0; i < grids.size(); i++) {
fwrite(&grids[i], (int) ceil((nx * ny) / 8), 1, cFile);
}
fclose(cFile);
This part works fine.
However, when I try to read from a file, all of the grids are -858993460, regardless of size, although it gets the number of grids correct. My code for reading:
FILE *cFile;
if (fopen_s(&cFile, ("grid_" + std::to_string(nx) + "_c.bin").c_str(), "rb") != 0) return;
fseek(cFile, 0, SEEK_END);
long size = ftell(cFile);
int grids = size / ((nx * ny) / 8);
for (int n = 0; n < shapes; n++) {
__int64 data;
fread(&data, (int) ceil((nx * ny) / 8), 1, cFile);
printf("%i\n", data);
}
fclose(cFile);
What am I doing wrong?
If you need any more information to answer, leave a comment and I'll give it to you.
Thanks in advance!
ceilhere?ceilwould round down, so 603 would end up as 91.