Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6241203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:43:12+00:00 2026-05-24T11:43:12+00:00

This is somewhat related to my recent post about Fortran stream and the like:

  • 0

This is somewhat related to my recent post about Fortran stream and the like: Converting data stored in Fortran 90 binaries to human readable format.

I am attempting to write a simple array of integers to a file, and then use Fortran’s READ function to then read in the binary that I have created. I am using stream by including ACCESS="STREAM" in my OPEN directive. I have the following code:

MODULE streamtest2subs

  IMPLICIT NONE

CONTAINS

SUBROUTINE writeUstream(myarray)
  IMPLICIT NONE
  INTEGER, INTENT(IN), DIMENSION(4,10) :: myarray
  INTEGER :: myvalue = 12345, mypos
  OPEN(UNIT=11, FILE="ustream.demo", STATUS="REPLACE", ACCESS="STREAM")
  WRITE(11) myarray
  CLOSE(UNIT=11)
END SUBROUTINE writeUstream

SUBROUTINE readUstream
  IMPLICIT NONE
  INTEGER :: test1, test2, test3
  INTEGER :: n
  OPEN(UNIT=42, FILE="ustream.demo", STATUS="OLD", ACCESS="STREAM")
  READ(42, POS=1) test1
  READ(42, POS=2) test2
  READ(42, POS=3) test3

  WRITE(*,*) "This is the output:"
  WRITE(*,*) test1
  WRITE(*,*) test2
  WRITE(*,*) test3
END SUBROUTINE readUstream

END MODULE streamtest2subs

PROGRAM streamtest2
  USE streamtest2subs
  IMPLICIT NONE
  INTEGER :: i, j, k
  INTEGER, DIMENSION(4,10) :: a

  WRITE(*,*) "This is my input array:"
  k=1
  DO i=1,4
    DO j=1,10
      a(i,j)=k
      WRITE(*, "(i3)", ADVANCE="NO") a(i,j)
      k=k+1
    END DO
    WRITE(*,*)
  END DO
  WRITE(*,*)

  CALL writeUstream(a)
  CALL readUstream
END PROGRAM streamtest2

However, when I compile this using gfortran and run it, I get the following output:

 This is my input array:
  1  2  3  4  5  6  7  8  9 10
 11 12 13 14 15 16 17 18 19 20
 21 22 23 24 25 26 27 28 29 30
 31 32 33 34 35 36 37 38 39 40

 This is the output:
           1
   184549376
      720896

Why is it that the output is so complex? Is it that READ is reading the ustream.demo file as a string instead of as an integer? However, when I change the type of test1, test2, and test3 to string, my output is simply a series of three blank lines.

Am I using the POS directive in READ incorrectly? I think that POS specifies the character number in the output (although I am not sure if the elements of the array are delimited in any way); is this correct?

Thank you very much for your time!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:43:13+00:00Added an answer on May 24, 2026 at 11:43 am

    For the purposes that you have described in your previous question, I think that you are programming this more complicated than is necessary. Assuming that you want a binary file without extra record structure, you want unformatted and stream. You can read the file in the same way that you wrote it — you don’t need to use POS — unless the purpose is to learn how to use POS.

    My tested example is based on a Fortran program in which I read a binary file that was written by someone else, almost certainly with a C program. The file consists of headers, followed by arrays of variable lengths. I open the file:

    open ( unit=75, file=FileName, status='old', access='stream', form='unformatted', action='read' )
    

    I read a header (a variable that is a user-define type with many sub-variables):

    read (75) header
    

    I allocate the array to the length that was read into one field of the header, then I read the array:

    allocate ( array (1:header % ArrayLen) )
    read (75) array
    

    Then I process the data in the array.
    Then I repeat until end of file (not shown in the code samples).

    Very easy … no need to calculating position in the file and use the POS keyword of READ.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somewhat related to this question , but in the absence of any answer about
My problem is somewhat related to this question. I have a data as below
This is somewhat related to a similar post , but that post was Visual
this is somewhat related to the post in: Perform OR on two hash outputs
This is somewhat related to the question posed in this question but I'm trying
This query is somewhat related to this earlier one on sorting where, it was
This issue is somewhat related: Problem with Code Generated by XSD.EXE: Sequence of Elements
This question is related somewhat to the one i asked HERE . Now, i
This is not 100% programming related. But I think this is somewhat useful because
this is somewhat of an odd question. I wrote a C function. Its 'like'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.