Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Firstly, I do not think you need to close and reopen your output file as you are doing. The <code>status</code> specifier is only relevant to the <code>open</code> statement in which it appears: <code>replace</code> will delete <code>Newfile.bin</code> if it exists at that time, before opening a new file with the same name. The status is implicitly changed to <code>old</code>, but this does not affect any operations done to the file.</p> <p>However, since your Fortran code does not know you run it 12 times, you should have a way of making sure the file is only replaced the first time and opened as <code>old</code> afterwards; otherwise, <code>Newfile.bin</code> will only contain the information from the last file processed.</p> <p>As for reading in the wrong values, this most likely occurs because of the difference between direct access (where you can choose a record length) and stream access (where you cannot). With stream access, data is stored as a sequence of "file storage units". Their size is in general compiler-dependent, but is available through the module <code>iso_fortran_env</code> as <code>file_storage_size</code>; it is usually 8 bits. This means that each entry will usually occupy multiple storage units, so you have to take care that a read or write with the <code>pos =</code> specifier does not access the wrong storage units.</p> <hr> <p><strong>Edit:</strong><br> Some example code writing and reading with stream access:</p> <pre><code>program stream use, intrinsic :: iso_fortran_env implicit none integer :: i, offset real(real32), dimension(4,6) :: val, nval open(unit=2, file='Newfile.bin', action='readwrite', form='unformatted', &amp; access='stream', status='replace') do i = 1,2 call random_number(val) write(2) val enddo ! The file now contains two sequences of 24 reals, each element of which ! occupies the following number of storage units: offset = storage_size(val) / file_storage_size ! Retrieve the second sequence and compare: read(2, pos = 1 + offset*size(val)) nval print*, all(nval == val) close(2) end program </code></pre> <p>The value <strong>true</strong> should be printed to the screen.</p> <p>Note also that it's not strictly necessary to specify a <code>pos</code> while writing your data to the file, because the file will automatically be positioned beyond the last record read or written.</p> <hr> <p>That said, direct or stream access is most beneficial if you need to access the data in a non-sequential manner. If you only need to combine input files into one, it could be easier to write the output file with sequential access, for which you can also specify <code>recl</code> and <code>position = 'append'</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload