Skip to content

bug fix for skipping zero length record #7

@taotaokai

Description

@taotaokai

Problem

One of my Fortran progame may write out a record of an empty array (for example, when no data point was found). When I try to read back the fortran file and skip this empty record using read(f) I got "attempting to read beyond record end" error. Reading the same file in Fortran has no problem.

Possible solution

In the src/withsubrecord.jl:

 49 function advance!( rec::RecordWithSubrecords )                                            
 50    #subreclen, sign = rdmarker(rec) # read trailing record marker                         
 51    #if subreclen != rec.subreclen                                                         
 52    #   fthrow("trailing subrecord marker doesn't match")                                  
 53    #end                                                                                   
 54    #**FIX HERE** advance to the next subrecord only when rec.more is true                                  
 55    if rec.more                                                                            
 56       subreclen, sign = rdmarker(rec) # read trailing record marker                       
 57       if subreclen != rec.subreclen                                                       
 58          fthrow("trailing subrecord marker doesn't match")                                
 59       end                                                                             
 60       subreclen, more = rdmarker(rec) # read next leading subrecord marker                
 61       rec.subleft = rec.subreclen = subreclen                                             
 62       rec.more = more                                                                     
 63    end                                                                                    
 64    nothing                                                                                
 65 end 

...

111 function Base.close( rec::RecordWithSubrecords )
112    if rec.writable
113       @assert rec.totleft == 0
114       @assert rec.subleft == 0
115       @assert !rec.more
116       wrmarker(rec, rec.subreclen, !rec.isfirst)
117    else
118       while rec.subleft > 0
119          skip(rec.io, rec.subleft)
120          rec.subleft = 0
121          advance!(rec)
122       end
123       @assert !rec.more
124       #**FIX HERE** read trailing record marker of the last subrecord
125       subreclen, sign = rdmarker(rec)
126       if subreclen != rec.subreclen
127          fthrow("trailing subrecord marker doesn't match")
128       end
129    end
130    nothing
131 end

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions