Loading...
You are not logged-in Login/Register





  • Posts   Search Threads
  • efengleNovember 4, 2011 3:35 PM PDT   
    EOF with Stream I/O

    I have a very simple example as a way to ask this question.
    program test
    implicit none
    
    integer(KIND=8) :: val
    open(unit=10,file="fort.10",form="unformatted",access="stream",status="old")
    read(10,POS=1)val
    write(6,*)'VAL = ',val
    close(10)
    stop
    end program test
    The file being read, fort.10, was created by another simple FORTRAN program.  That program wrote a 4-byte integer with a value of 65 as stream output, so there is no FORTRAN header/trailer record information in the file.
    When the above program is executed, I get an EOF error.  I think I can understand why.  Does the READ statement instrinsically try to fill all N-bytes of the variable (8 in this case) from the input file?  Recall the input file only contains 4-bytes
    Is there a way to specify how many bytes I want to READ statement to read?
    This is just a test case, but ultimate goal I am working on is reading variable length records.
    Here is the program to create the file.
    program test_write
    implicit none
    
    integer(KIND=4) :: val
    open(unit=10,form="unformatted",access="stream",status="new")
    val=65
    write(10)val
    close(10)
    stop
    end program test_write
    
    Thanks,
    Eric


    mecej4November 5, 2011 2:51 AM PDT
    Rate
     
    EOF with Stream I/O

    For unformatted and stream I/O, the number of 'storage units' read/written is determined by the size of the entities in the I/O list. The types of the variables in the I/O list are not significant, only their cumulative size. Thus, your example is hardly different from one in which you wrote ten bytes into a file and tried to read twenty using CHARACTER(len=10) and CHARACTER(len=20) variables.

    In your example, the READ requires eight or more bytes to be present in the remainder of the file. Trying to read more bytes than present in the file will cause EOF. You can catch and process the EOF, but note that some implementations may not let you rewind or backspace stream files.


    efengleNovember 5, 2011 1:25 PM PDT
    Rate
     
    EOF with Stream I/O

    Funny how it makes more sense when someone else explains it to you.  Anyways, at work we use IBM XL Fortran.  They have a specific IBM extension in the READ statement.  You can specify a NUM=<integer variable> keyword specifies the number of bytes to transfer between the I/O list and the file.
    That would be a great feature to add the Intel Fortran.


    jimdempseyatthecoveNovember 5, 2011 3:41 PM PDT
    Rate
     
    EOF with Stream I/O

    What is this supposed to do with the rest of the I/O list?
    Zero fill? Don't touch?

    What do you want it to do when reading unformatted REAL(4) into REAL(8)?

    For your purpose you could read into a character array, then pick 1, 2, 4, 8, 16, ... bytes as you wish
    Use TRANSFER or UNION to "cast" types from character to type of your choice.

    Jim Dempsey

    Blog: The Parallel Void
    www.quickthreadprogramming.com

    mecej4November 5, 2011 4:12 PM PDT
    Rate
     
    EOF with Stream I/O

    If you must do byte-juggling in a Fortran application:

    Many Fortran compilers let you call routines in the system C runtime, such as fread() and fwrite(), and the C-interoperability of F2003 makes that easier.

    Whether to use a vendor extension to Fortran or to use standard C is a portability issue whose costs/benefits need to be assessed.

Forum jump:  

Intel Software Network Forums Statistics

17,025 users have contributed to 48,319 threads and 172,758 posts to date.

In the past 24 hours, we have 11 new thread(s) 54 new posts(s), and 47 new user(s).

In the past 3 days, the most popular thread for everyone has been Optimalization of sine function\'s taylor expansion The most posts were made to Most likely, the issue is that The post with the most views is Optimalization of sine function\'s taylor expansion

Please welcome our newest member redfruit83


For more complete information about compiler optimizations, see our Optimization Notice.