I have a typedef as follows:
I do the following to create an array of foo:
I fill the values of bar with random values in the first position and zero in the second position as follows:
Then, later on, I want to write the array to disk in binary form, so I do:
However, when I later read the file back in, as follows:
I end up with a different array.
I've tried using
but the results are the same (identical, but wrong).
What am I missing?
Code:
typedef double foo[2];
I do the following to create an array of foo:
Code:
foo* bar = (foo*)malloc(sizeof(foo) * arraySize);
I fill the values of bar with random values in the first position and zero in the second position as follows:
Code:
for(unsigned int i = 0; i < arraySize; i++)
{
bar[i][0] = randomFunctionReturningDouble();
bar[i][1] = 0;
}
Then, later on, I want to write the array to disk in binary form, so I do:
Code:
FILE* outputFile = fopen("file.bin","w");
fwrite(bar, sizeof(foo) * arraySize, 1, outputFile);
fclose(outputFile);
However, when I later read the file back in, as follows:
Code:
foo* bar = (foo*)malloc(sizeof(foo) * arraySize);
FILE* inputFile = fopen("file.bin","w");
fread(bar, sizeof(foo) * arraySize, 1, inputFile);
fclose(inputFile);
I've tried using
Code:
fwrite(bar, sizeof(foo), arraySize, outputFile);
What am I missing?