Reputation: 836
I am reading a binary file into a set of external text files for post-processing. My code is as follows:
for(k = 0; k < 76; k++) {
for(j = 0; j < 76; j++) {
curr = curr + j + k*(dim - 76);
sprintf(Filename, "%s_%d.txt", file,count);
fp = fopen(Filename, "w");
for(i = 0; i < 256; i++) {
fseek(qFile, (curr + i*dim*dim) * sizeof(float), SEEK_SET);
fprintf(oFile, "%f\n", curr + i*dim*dim);
fread(&temp, sizeof(float), 1 ,qFile);
fprintf(fp, "%5.2f\n", temp);
}
fclose(fp);
count++;
}
}
However, when I view the file pointed to by oFile, the beginning locations of each set of 256 values are not spaced by 1, but are as follows:
67024216
67024216
67024216
67024220
67024224
67024228
67024232
67024240
67024248
67024256
67024264
67024276
67024288
67024300
67024312
.
.
.
I cannot see why the spacing in the individual elements is in 4's, I know the size of the float is 4bytes, but the value that is being printed to file is not the number of bytes, but the multiple of the number of bytes.
Upvotes: 0
Views: 63
Reputation: 836
Code edited as follows produced the desirable result:
for(k=0;k<76;k++){
curr = init+k*dim;
for(j=0;j<76;j++){
curr = init+j+k*dim;
sprintf(Filename,"%s_%d.txt",file,count);
fp=fopen(Filename,"w");
for(i=0;i<256;i++){
fseek(qFile,(curr+i*dim*dim)*sizeof(float),SEEK_SET);
fprintf(oFile,"%lf\n",curr+i*dim*dim);
fread(&temp,sizeof(float),1,qFile);
fprintf(fp,"%5.2f\n",temp);
}
fclose(fp);
count++;
}
}
Upvotes: 0
Reputation: 1981
Single-precision floating-point numbers don't work like integers. They actually have less precision, because eight bits are used for the sign and exponent. That leaves you with 24 bits.
And all of your numbers happen to fit into 24 bits. Or, rather, they would if you lopped off the low two bits, which happen to always be zeroes (i.e., increasing by multiples of 4).
I don't know if it'll solve all of your problems, here, since I obviously don't know the details of the project. But, if you change your float
definitions to double
, it should at least get you past this issue.
Upvotes: 1