I have a binary file and I will be using fread to read the data from this binary file into an array of structures.
However, I don't know what value to pass to fread as its second argument. I know the file size is 536870912 bits. The binary file was constructed on the basis of being accessed for a 512^3 array. This means each data entry is of type float in the binary file with 4 bytes specified for each data element.
I made an error with the mention of bits. I read what was outputted by a C program finding the size of the file - it outputted 536870912 bits! Apologies to anyone confused.
Here is the code i'm using to read the data from the binary file into my arrary of structures (a simplified structure - there are 10 other parameters!)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// Define the model structure
struct model {
float density;
};
// Entry point for the program
int main () {
int counter;
long lSize;
char * buffer;
size_t result;
FILE *pFile;
int i,j,k,ibox; /* Loop indices for the physical grid */
struct model ***mymodel;
pFile = fopen("core1_dens_0107.bin","rb");
if (pFile == NULL) { printf("Unable to open density file!"); exit(1); }
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
printf( "File size : %lu Bits \n", lSize );
for ( j = 0 ; j < 512 ; j++ ) {
for ( k = 0; k < 512; k++ ) {
for ( i = 0; i < 512; i++ ) {
fread(&mymodel[i][j][k].density,4,1,pFile);
printf("%f \n",mymodel[i][j][k].density);
}
}
}
fclose(pFile);
return 0;
}