comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » read a C written binary file with IDL
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
read a C written binary file with IDL [message #79238] Thu, 09 February 2012 19:44 Go to next message
Thibault Garel is currently offline  Thibault Garel
Messages: 55
Registered: October 2009
Member
Hi,

I am having a problem with reading a C written binary file with IDL.
It may come from differences of type definitions between C and IDL but
I could not really figure out from Google...

In C, it writes a structure containing the following variable types:

struct MyStruct
{
int a;
long long b;
int c;
float d;
};

Then, in IDL, I read this with:

MyStruct = {$
a : 0L, $
b : 0LL, $
c : 0L, $
d : 0.0 $
}

openr, 1, filename, /SWAP_IF_BIG_ENDIAN
readu, 1, MyStruct
close, 1


but this gives me wrong values.

Did I miss something about the type conversion??

If someone could please clarify this, it would really help!
Thanks !
Re: read a C written binary file with IDL [message #79309 is a reply to message #79238] Wed, 22 February 2012 05:24 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
bing999 writes:

> Actually I cannot use assoc i guess. To be fully complete this
> time :), the binary file contain a header (some int) + the structure
> mystruct which is replicated N times. From what I understand of the
> assoc function, it won't work in my case...

I think your understanding must be faulty, as this
is *exactly* the kind of file ASSOC works best with. :-)

The "offset" is used to account for the header.

Cheers,

David



--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: read a C written binary file with IDL [message #79311 is a reply to message #79238] Tue, 21 February 2012 21:45 Go to previous messageGo to next message
Thibault Garel is currently offline  Thibault Garel
Messages: 55
Registered: October 2009
Member
Hey,

Actually I cannot use assoc i guess. To be fully complete this
time :), the binary file contain a header (some int) + the structure
mystruct which is replicated N times. From what I understand of the
assoc function, it won't work in my case...
And the problem is that i cannot changer the compiling options of the
C code...


cheers
Thibault


>
>
>> Hi,
>
>> sorry to reply so late, but I was away recently.
>
>> First, as the issue seems to be more complicated than i first thought,
>> i have to say that the structure i have to read is bigger than what I
>> wrote previously just to make an example.
>
>> It actually contains (in the right order):
>> int x 1
>> long long x 1
>> int x 5
>> float x 9
>> int x 1
>> float x 24
>
>> that is, 7 int, 33 float and 1 long long (written in C).
>
>> When I look at the IDL commands you suggest, i get:
>
>>   print,n_tags(mystruct,/length) 176
>
>>   print,n_tags(mystruct,/data_length) 172
>
>> So you were right.
>
>> Now, the question is "How to handle that?? " :)
>
>> I inserted an extra int (0L) in the structure of my IDL reading
>> routine in second position of the structure (just before the long
>> long), and it gives results coherent with what one gets with a correct
>> Python routine. However, I am not quite satisfied with this
>> solution...
>
>> Does anyone can think of a better way to proceed?
>
>> Thanks !!
>
>>> Bill Nel wrote:
>>>> On Feb 9, 11:56 pm, Manodeep Sinha <manod...@gmail.com> wrote:
>>>> > Hi,
>
>>>> > This is because C pads the structure to produce alignments. Under
>>>> > 'normal' operations, you would expect MyStruct to be 20 bytes,
>>>> > however, if you do a sizeof(struct MyStruct), you will probably see
>>>> > that the size is 24. (And you can enable the warning for gcc by using
>>>> > the compile time option -Wpadded).
>
>>>> The N_TAGS() function has LENGTH and DATA_LENGTH keywords:
>
>>>> IDL> print, n_tags(mystruct, /length)
>>>>           24
>>>> IDL> print, n_tags(mystruct, /data_length)
>>>>           20
>
>>>> I mention it because one might not think to look at the N_TAGS
>>>> function
>>>> for this sort of information.
>
>>>> --Wayne
>
> As Paul suggested, you should check out the assoc function. I can read
> the data in just fine through IDL:
>
> ---------------
> openu, 1, 'data.bin', /SWAP_IF_BIG_ENDIAN
> a = assoc(1, {a:0L, b:0LL, c:lonarr(5),d:fltarr(9),e:0L,f:fltarr(24)})
> print,a[0]
> --------------
>
> Cheers,
> Manodeep
Re: read a C written binary file with IDL [message #79335 is a reply to message #79238] Mon, 20 February 2012 19:43 Go to previous messageGo to next message
manodeep@gmail.com is currently offline  manodeep@gmail.com
Messages: 33
Registered: June 2006
Member
On Feb 20, 7:49 pm, bing999 <thibaultga...@gmail.com> wrote:
> Hi,
>
> sorry to reply so late, but I was away recently.
>
> First, as the issue seems to be more complicated than i first thought,
> i have to say that the structure i have to read is bigger than what I
> wrote previously just to make an example.
>
> It actually contains (in the right order):
> int x 1
> long long x 1
> int x 5
> float x 9
> int x 1
> float x 24
>
> that is, 7 int, 33 float and 1 long long (written in C).
>
> When I look at the IDL commands you suggest, i get:
>
>   print,n_tags(mystruct,/length) 176
>
>   print,n_tags(mystruct,/data_length) 172
>
> So you were right.
>
> Now, the question is "How to handle that?? " :)
>
> I inserted an extra int (0L) in the structure of my IDL reading
> routine in second position of the structure (just before the long
> long), and it gives results coherent with what one gets with a correct
> Python routine. However, I am not quite satisfied with this
> solution...
>
> Does anyone can think of a better way to proceed?
>
> Thanks !!
>
>
>
>
>
>
>
>
>
>> Bill Nel wrote:
>>> On Feb 9, 11:56 pm, Manodeep Sinha <manod...@gmail.com> wrote:
>>>> Hi,
>
>>>> This is because C pads the structure to produce alignments. Under
>>>> 'normal' operations, you would expect MyStruct to be 20 bytes,
>>>> however, if you do a sizeof(struct MyStruct), you will probably see
>>>> that the size is 24. (And you can enable the warning for gcc by using
>>>> the compile time option -Wpadded).
>
>>> The N_TAGS() function has LENGTH and DATA_LENGTH keywords:
>
>>> IDL> print, n_tags(mystruct, /length)
>>>           24
>>> IDL> print, n_tags(mystruct, /data_length)
>>>           20
>
>>> I mention it because one might not think to look at the N_TAGS
>>> function
>>> for this sort of information.
>
>>> --Wayne

As Paul suggested, you should check out the assoc function. I can read
the data in just fine through IDL:

---------------
openu, 1, 'data.bin', /SWAP_IF_BIG_ENDIAN
a = assoc(1, {a:0L, b:0LL, c:lonarr(5),d:fltarr(9),e:0L,f:fltarr(24)})
print,a[0]
--------------

Cheers,
Manodeep
Re: read a C written binary file with IDL [message #79582 is a reply to message #79309] Sun, 11 March 2012 19:49 Go to previous message
Thibault Garel is currently offline  Thibault Garel
Messages: 55
Registered: October 2009
Member
Hi all,
thanks for your help. Indeed, the assoc function works fine (without
using the /packed keyword).
Cheers
Bing


> bing999writes:
>> Actually I cannot use assoc i guess. To be fully complete this
>> time :), the binary file contain a header (some int) + the structure
>> mystruct which is replicated N times. From what I understand of the
>> assoc function, it won't work in my case...
>
> I think your understanding must be faulty, as this
> is *exactly* the kind of file ASSOC works best with. :-)
>
> The "offset" is used to account for the header.
>
> Cheers,
>
> David
>
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming:http://www.idlcoyote.com/
> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: type conversion in GPULIB
Next Topic: Matlab!

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:18:04 PDT 2025

Total time taken to generate the page: 0.00548 seconds