T O P I C R E V I E W |
bnholland |
Posted - 12/19/2007 : 10:56:29 AM Origin Version (Select Help-->About Origin): 7.5 SR4 Operating System: Win XP
Hi,
I am trying to load a binary file which contains a header structure with various different data types using fread. On loading this it assigns all values correctly up to the point where it is trying to load a double value. The double value it reads in is offset by 4 bytes. If I add into the structure an extra place taking variable of 4 bytes the double still takes exactly the same value (correct in this case). It seems to me that the double is always read in some multiple of 8 number of bytes from the start of the file.
E.g. For the structure
struct example { long number1; //4 bytes double number2; //8 bytes }
the binary line:
01 00 00 00 00 00 00 00 00 00 00 0F 3F 00 00 00 00
is being read as
example.number1 = 1 example.number2 = 5.294.....e-315 instead of 1 (00 00 00 00 00 00 0F 3F which I want to read it as is 1)
And it gives the same value for number2 as reading the structure struct example2 { long number1; long padding; long number2; }
Does anyone know why it is not reading correctly and how I might read the above line from a file into a structure as I would like (i.e. example.number1 = 1, example.number2 = 1.0)?
Many Thanks Brendan
Edited by - bnholland on 12/19/2007 11:07:46 AM
Edited by - bnholland on 12/19/2007 11:11:43 AM |
5 L A T E S T R E P L I E S (Newest First) |
bnholland |
Posted - 01/07/2008 : 06:22:59 AM Thanks very much, that was indeed the problem. As always, a simple and stupid cause to a confusing problem.
|
cpyang |
Posted - 12/28/2007 : 09:31:44 AM Check how you had open the file with,
stream = fopen(strfilename, "rb" );
not
stream = fopen(strfilename, "rt" );
CP
|
Mike Buess |
Posted - 12/20/2007 : 1:13:14 PM That does seem to be related to the other problem but it's serious enough that it should be addressed by OriginLab's technical support team <tech@originlab.com>. They will want to see one of your data files as well as your import function. It's also probably a good idea to reference this forum topic in your email message.
Mike Buess Origin WebRing Member
Edited by - Mike Buess on 12/20/2007 1:14:02 PM |
bnholland |
Posted - 12/20/2007 : 12:11:16 PM Thanks very much Mike for your help there. That has solved one problem and revealed another of a yet more mysterious nature. The file contains a list of data values stored as doubles. If I read the data in sequentially using fread in a loop it loads up to a certain point and then fails to read in any more data values. I can force a solution to this in most cases by using fseek prior to each fread.
E.g. 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 .... would load as 1 2 3 3 3 3 3 without using seek each time (except that for my long test file it loads 300 values before it encounters the problem).
However, for data stored as doubles this yields another problem which may or may not be related. The result is even stranger on my test case there. It fails to load the first 76 values correctly (the last two bytes of the number are read as 00 00), loads the next 62 values correctly, then the last 72 load with a slight offset to each data value (which corresponds to reading the 56th bit as a 1 instead of 0, all other bits correct). NB the same bit errors in reading the data exist if I do so as blocks of 8 chars instead.
Any ideas as to what the problem is caused by, what else to test and ideally how to solve it? |
Mike Buess |
Posted - 12/19/2007 : 1:01:50 PM Hi Brendan,
I ran across a similar problem a few years ago and it appeared that doubles were read 8x bytes from the start of the struct rather than file. My solution was to start a new struct whenever datatype changed to double. The solution for your example is simply this...
struct example1 { long number1; }
struct example2 { double number2; }
I hope that helps.
Mike Buess Origin WebRing Member |