I'm trying to write some arrays (of reals) to a file.
open(unit=100, file = 'file.dat', status = 'replace')
do j = 1, nss
write(unit=100, *) m(j)!, xs(j), ys(j), zs(j)
end do
close(unit=100)
However, this gives me:
Error: Syntax error in WRITE statement at (1)
What an I doing wrong? Perhaps it's linked to some previous read and write statements? Because this looks legit to me.
Thanks to @lastchance. It worked replacing write(unit=100, *)
with write(100, *)
The WRITE statement (and one form of the READ statement) takes a control information list to say how the data transfer goes about, and a data transfer output list (input for READ) to say which data entities are transferred. The control information list is the first bit, in parentheses.
A control information list is a list of specifiers1, like unit=100
, fmt='(A)'
, and iostat=status
. For all but the unit, format and namelist specifiers the spec=...
part is necessary.
For the unit specifier, UNIT=
is optional if the unit is the first item of the list. For the format and namelist specifiers, the FMT=
and NML=
characters are optional if the specifier is the second item in the list (only one of those specifiers may be give) and the unit is specified without UNIT=
(Fortran 2018 C1217, C1218):
If format appears without a preceding FMT=, it shall be the second item in the io-control-spec-list and the first item shall be io-unit.
and
If namelist-group-name appears without a preceding NML=, it shall be the second item in the io-control-spec-list and the first item shall be io-unit.
So, as noted, correct forms would be:
write(unit=100, fmt=*) ...
write(fmt=*, unit=100) ...
write(100, *) ...
write(100, fmt=*) ...
1 Despite appearances, there is little connection between specifiers and a procedure's arguments. A specifier like unit=2
is not a keyword argument and a specifier like *
is not a positional argument.