×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

hi im trying to read an unformatted

hi im trying to read an unformatted

hi im trying to read an unformatted

(OP)
hi im trying to read an unformatted UNIX binary file generated by a software... its suppose to contain information but i cant read it and its my first time dealing with this kind of file format
the software maual has this "The files are UNIX-Fortran unformatted binary files, even on the Windows platform, according
to the general ECLIPSE standard, to ensure platform independence. Furthermore, all keywords
defined in this document that have indexes use base 1 in order to be compatible with all other
ECLIPSE output formats."
file links(my code to read it):http://files.engineering.com/getfile.aspx?folder=b...
file format:http://files.engineering.com/getfile.aspx?folder=9...
file:http://files.engineering.com/getfile.aspx?folder=2...

RE: hi im trying to read an unformatted

To read a binary file, do not use a formatted read. Your read statement should look something like

CODE

read(11) key, data_count, data_type 
There is no format specification.

It looks like your data is written in 16 byte records.

RE: hi im trying to read an unformatted

(OP)
i'm not very good at this and i'm the only person tasked with reading this file so can you show me an example of data_count and type
program MAIN
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_INT
implicit none
INTEGER, PARAMETER :: unit = 18
integer::ii,jj,kk
real(8)::INTEHEAD,INTE,GEOMETRY,DOUB
CHARACTER(*), PARAMETER :: filename = 'TEST1604.dat'
character::str*500
INTEGER(kind=4) :: data,c
OPEN(UNIT=18,FILE='TEST1604.dat', ACCESS='stream',
FORM='UNFORMATTED',CONVERT='BIG_ENDIAN',STATUS='OLD')
DO ii=1,3
READ (18) data
END DO
CLOSE(18)
OPEN(UNIT=18,FILE='new.dat', ACCESS='stream',
FORM='UNFORMATTED',CONVERT='BIG_ENDIAN',STATUS='OLD')
write(18)
end program MAIN
this is the changes i've made so far but still im getting jibberish reading

RE: hi im trying to read an unformatted

Try this. What you have to understand is that each read will read 1 record. For instance

CODE --> items

read(in_file) key, count, data_type  ! read 1 record

! read 3 records
read(in_file) key
read(in_file) count
read(in_file) data_type 

This is easy enough to work out for normal data but for arrays

CODE --> arrays

read(in_file) (xxx(ii), ii = 1, count)   ! read 1 record

! read count records
do ii = 1, count
    read(in_file) xxx(ii)
end do 

The problem with your data is that it has a maximum size for array reads. It looks like the value is 1000 so the data has to be read in blocks of 1000 if the count is more than 1000. Have a play with this - the data will be read into an array of rectype. From there you have to write the code to decide what to do with each record type

CODE

module modbin
    type rectype
        character(len=8)::key
        integer::data_count
        character(len=4)::data_type
        logical::is_int
        integer, allocatable:: idata(:)
        real(kind=8), allocatable::rdata(:)
    end type
contains
    subroutine rec_read(in_file, out_rec)
        integer, intent(in):: in_file
        type (rectype), intent(inout):: out_rec
        !
        ! You need to play around with this figure.  It may not be
        ! entirely accurate - 1000 seems to work, 1024 does not
        integer, parameter:: bsize = 1000
        integer:: bb, ii, iimax

        ! read the header
        out_rec%data_count = 0
        out_rec%data_type = '    '
        read(in_file, end = 20) out_rec%key, out_rec%data_count, out_rec%data_type
        ! what type is it?
        select case (out_rec%data_type)
        case ('INTE')
            out_rec%is_int = .true.
            allocate(out_rec%idata(out_rec%data_count))

        case ('DOUB')
            out_rec%is_int = .false.
            allocate(out_rec%rdata(out_rec%data_count))
        end select

        ! read the data in blocks of bsize
        bb = 1
        do while (bb .lt. out_rec%data_count)
            iimax = bb + bsize - 1
            if (iimax .gt. out_rec%data_count) iimax = out_rec%data_count
            if (out_rec%is_int) then
                read(in_file) (out_rec%idata(ii), ii = bb, iimax)
            else
                read(in_file) (out_rec%rdata(ii), ii = bb, iimax)
            end if
            bb = iimax + 1
        end do
20      continue
    end subroutine rec_read

    subroutine rec_print(in_recnum, in_rec)
        integer, intent(in):: in_recnum
        type (rectype), intent(in):: in_rec
        print *, in_recnum, in_rec%key, in_rec%data_count, in_rec%data_type
        ! print out data

    end subroutine rec_print
end module modbin

program main
    use modbin
    integer, parameter:: infile=11
    ! fixed size for now - should really be allocatable
    integer, parameter:: rrmax = 50
    type (rectype):: rec(rrmax)
    integer:: rr, rlast

    open(unit=infile, file='TEST1603.SLN0001', form='UNFORMATTED', status='OLD', convert='BIG_ENDIAN')
    rlast = 0
    do rr = 1, rrmax
        call rec_read(infile, rec(rr))
        if (rec(rr)%data_type .eq. '    ') exit
        rlast = rr
        call rec_print(rr, rec(rr))
    end do
    print *, 'No more records'
    close(infile)
end program main 

RE: hi im trying to read an unformatted

(OP)
im still getting errors when i run the code. it says unformatted record is too short for input list

RE: hi im trying to read an unformatted

I tried it with gfortran on linux. Which compiler are you using?

RE: hi im trying to read an unformatted

(OP)
GNU fortran compiler

RE: hi im trying to read an unformatted

(OP)
what happened when you run it?.... where you able to see the contents of the file?

RE: hi im trying to read an unformatted

I am assuming that the datafile is in the same directory as the executable and that you are running it from the cmd line. Also assuming you are not double clicking to run it. Unless you change the filename to include the actual filepath, that won't work. I'll see if I can find the windows version. Is it TEST1603.SLN0001 you are trying to read? On Linux, I am getting

CODE --> results

1 INTEHEAD          10 INTE
           2 GEOMETRY       22536 DOUB
           3 GEOMINDX         301 INTE
           4 ID_BEG           300 INTE
           5 ID_END           300 INTE
           6 ID_CELL         7212 INTE
           7 TIME_BEG        7512 DOUB
           8 SWAT            7212 DOUB
 No more records 

Same result on Windows. See if you can read the first record - you should get INTEHEAD ... If you are still getting

CODE --> readone.f90

module modbin
    type rectype
        character(len=8)::key
        integer::data_count
        character(len=4)::data_type
        logical::is_int
        integer, allocatable:: idata(:)
        real(kind=8), allocatable::rdata(:)
    end type
contains
    subroutine rec_read(in_file, out_rec)
        integer, intent(in):: in_file
        type (rectype), intent(out):: out_rec

        ! read the header
        read(in_file, end = 20) out_rec%key, out_rec%data_count, out_rec%data_type
20      continue
    end subroutine rec_read

    subroutine rec_print(in_rec)
        type (rectype), intent(in):: in_rec
        print *, in_rec%key, in_rec%data_count, in_rec%data_type
        ! print out data

    end subroutine rec_print
end module modbin

program main
    use modbin
    integer, parameter:: infile=11
    type (rectype):: rec

    open(unit=infile, file='TEST1603.SLN0001', form='UNFORMATTED', status='OLD', convert='BIG_ENDIAN')
    call rec_read(infile, rec)
    call rec_print(rec)
    close(infile)
end program main 

Same results on Windows (MinGW-64 7.2.0) as Linux (5.4.0)

RE: hi im trying to read an unformatted

(OP)
i have codeblocks, tried plato, tried microsoft visual.... sorry im a newbie with these programs. if its no bother can you please walk me through step by step how you did it on windows and what softwares you used with the compilers.... thank you so much. for 3weeks this is the first time i have seen the contents of test1603

RE: hi im trying to read an unformatted

(OP)

RE: hi im trying to read an unformatted

g95 isn't gfortran. It is a totally different tool.
plato/silverfrost - I'll have to check on that when I get home
ms-visual - not sure if you mean powerstation4, cvf or ivf. IVF should be able to handle it.

For gfortran goto https://sourceforge.net/projects/mingw-w64/files/T...
Under MinGW-W64 GCC-7.2.0, select one of the compilers: I used x86_64-win32-seh. These are in 7z format so if you don't have 7zip, you will have to install that first. Copy the mingw files to a suitable place and add the mingw\bin directory to your path.

To compile "gfortran progname.f95"

RE: hi im trying to read an unformatted

(OP)

i was able to read it....thanks so much now i can read all the files. thank you so much. you've helped me so much

RE: hi im trying to read an unformatted

(OP)
can i please know your name? and if i want to add a write statement to this code to output another file with this data how will i add that?

RE: hi im trying to read an unformatted

(OP)
sorry for asking but i think there should be data under every heading having some data
example
INTEHEAD 10 INTE
2 1 3 1 20 10 1997 23 50 41 !10 numbers the last 6 is date and time
GEOMETRY 《a number》 DOUB like this

RE: hi im trying to read an unformatted

If you look at the code, in the routine rec_print - there is something that says ! print data. Left that for you to do.

RE: hi im trying to read an unformatted

I've just tried it with Silverfrost/Plato. It cannot handle BIG_ENDIAN. I can't find a mode where it can read the raw file. Guess Silverfrost/Plato is a no-go.

RE: hi im trying to read an unformatted

(OP)
thank you very much... I you helped me so much and I appreciate the help. if it wasn't for the help I wouldn't have gotten anything.
please if I may for another help... the data in between the headings are not showing. if the model is a 3D model it'll have XYZ values without the heading xyz in 2D it'll have XY.This is the manual of the file format
below is an example of a simple simulation. the simulation lines(the 3 lines)have coordinate readings under the heading GEOMETRY
This is how the information from the simulation above is recorded in the file SLN file
it is like plotted values on a graph sheet.now if it was a few lines I could manually record the values and use them in another simulator but a typical simulation looks like this. with this number of lines, I will have to rely on the values under those headings because there are too many of them to manually trace them.
please, do you think you can help me record those values under the headings?
Thanks again

RE: hi im trying to read an unformatted

(OP)
read all the data... thanks so much for all the help. have a nice day

RE: hi im trying to read an unformatted

Well done! Good to know you got there in the end.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close