On Saturday, November 22, 2014 6:31:27 PM UTC-5, Patrick O'Leary wrote:
>
> On Saturday, November 22, 2014 9:57:51 AM UTC-6, Isaiah wrote:
>>
>> Have you looked at StrPack.jl? It may have a packed option. Julia uses 
>> the platform ABI padding rules for easy interop with C.
>>
>
> Yes, you can used the align_packed strategy.
>

Hi Patrick,

I just checked out StrPack and installed it. I think I have it working 
properly in the case of a packed immutable type that just contains 
numerical fields. I'm still not sure how to make things work to unpack 
fixed length strings. If my string in field "e" is a fixed-length of 4, 
then my composite type looks like

@struct immutable TestType2
    a::Float64
    b::Int32
    c::Float32
    d::Int16
    e::Array{Uint8,1}(4)   # also tried ASCIIString(4)
end

And then I know my data on file has 10 records, I can loop through as:

data = Array(TestType2, (10,))

f = open("test1.dat", "r")
for k = 1:10
    data[k] = unpack(f, TestType2, {"a"=>8, "b"=>4,"c"=>4, "d"=>2, "e"=>4}, 
align_packed, :NativeEndian)
    println(data[k])
end

But Array{Uint8,1}(4) results in corrupted values after the first record is 
read in and ASCIIString(4) gives an error:

TestType2(-1.2538434657806456,0,0.5783242f0,0,"a")
ERROR: invalid ASCII sequence

Where in the first record that appears to be read correctly (at least it 
prints), the last field value should be "abcd" not "a". Am I missing 
something obvious? Also, is there a strategy for combining all of this with 
a memory mapped file?

I really appreciate all of the suggestions everyone has offered. It's my 
first foray into Julia and I'm guessing I'm starting out with something a 
little atypical. I'm hoping if I can get the data read then everything else 
should be much more straight forward. 

Reply via email to