Strange output and branch warning when compiling - fortran

I compile a fortran 77 code using gfortran and get the following error:
10 open (23,file=outfile,status='old',access='append',err=10)
1
Warning: Branch at (1) may result in an infinite loop
This happens several times.
One of the output files looks like the following:
^L6a10È <90> ) &<9b>LÓLÓLÕ<91><90> <90> <90> È <8e><9b>LÓLÓLÕ<93>2
!MERCURY ¢¤õ/!ô<8a><8a><90> ÿ<90> ÿ<90> ÿÌÖÏ©ü}M<91>
"VENUS «}>±{©±<8b><90> ÿ<90> ÿ<90> ÿʺ93¿<8d>d<91>
However, it should just look like a table of text.
Any ideas?

Your line of code
10 open (23,file=outfile,status='old',access='append',err=10)
specifies that the open statement should transfer control to itself (label 10) in case an error is encountered, so any error could trigger an infinite loop. It also suppresses the output of error messages. If you want to just check for an error status, I would suggest using the iostat and/or iomsg (Fortran 2003) arguments:
open (23, file=outfile, status='old', access='append', iostat=ios, iomsg=str)
Here ios is an integer that will be zero if no errors occur and nonzero otherwise, and str is a character variable that will record the corresponding error message.

The err= argument in your open statement specifies a statement label to branch to should the open fail for some reason. Your code specifies a branch to the line labelled 10 which happens to be the line containing the open statement. This is probably not a good idea; a better idea would be to branch to a line which deals gracefully with an error from the open statement.
The warning from gfortran is spot on.
As to the apparent garbage in your output file, without sight of the code you use to write the garbage (or what you think are pearls perhaps) it's very difficult to diagnose and fix that problem.

Related

Fortran read statement successful but calls err parameter also - why? [duplicate]

In a program I am improving, I noticed that Fortran does not detect file existing or not. This led to a logic error which has not been fixed. I highly appreciate if you could point out the problems or errors and give me your corrections.
open(unit=nhist,file=history,iostat=ierr)!This setting cannot exit program if file does not exist because ierr is always 0
if (ierr /=0) then
write(*,*)'!!! error#',ierr,'- Dump file not found'
stop
endif
!I used below statement, the program exits even though a file is existing
open(unit=nhist,file=history,err=700)
700 ierr=-1
if (ierr /=0) then
write(*,*)'!!! error#',ierr,'- Dump file not found'
stop
endif
There are two distinct problems here. Let's look at them separately.
First, consider
open(unit=nhist,file=history,iostat=ierr)
The comment suggests that ierr is always set to zero. Well, why shouldn't it be set to zero? ierr should be non-zero in the case the case of an error, but is the file not existing an error?
Not necessarily. In the absence of a status= specifier the default status='unknown' is taken. The compiler doesn't have to (and is unlikely to) treat opening in this case as an error if the file doesn't exist. It's likely to create it as needed on a write, or complain when trying to read.
Adding status='old' to the open statement is the usual way of saying "the file should exist".
Second, consider
open(unit=nhist,file=history,err=700)
700 ierr=-1
if (ierr /=0) then
...
If there's an error here, execution is transferred to the statement labelled 700. From this statement ierr is set to a non-zero value and off we go to the if construct to handle that error.
It's just that the statement labelled 700 also happens to be executed even without an error: it's simply the next statement after the open and there's no branch to miss it. [I could give an example of such branching, but I don't want to encourage use of err= in modern code. With the working iostat= things are far preferable.]
But if you just wish to test the existence of a file, consider inquire-by-file:
logical itexists
inquire (file=history, exist=itexists)
if (.not.itexists) error stop "No file :("
Some would argue this is even better than having status='old' in an open statement.

Cannot detect file existing or not

In a program I am improving, I noticed that Fortran does not detect file existing or not. This led to a logic error which has not been fixed. I highly appreciate if you could point out the problems or errors and give me your corrections.
open(unit=nhist,file=history,iostat=ierr)!This setting cannot exit program if file does not exist because ierr is always 0
if (ierr /=0) then
write(*,*)'!!! error#',ierr,'- Dump file not found'
stop
endif
!I used below statement, the program exits even though a file is existing
open(unit=nhist,file=history,err=700)
700 ierr=-1
if (ierr /=0) then
write(*,*)'!!! error#',ierr,'- Dump file not found'
stop
endif
There are two distinct problems here. Let's look at them separately.
First, consider
open(unit=nhist,file=history,iostat=ierr)
The comment suggests that ierr is always set to zero. Well, why shouldn't it be set to zero? ierr should be non-zero in the case the case of an error, but is the file not existing an error?
Not necessarily. In the absence of a status= specifier the default status='unknown' is taken. The compiler doesn't have to (and is unlikely to) treat opening in this case as an error if the file doesn't exist. It's likely to create it as needed on a write, or complain when trying to read.
Adding status='old' to the open statement is the usual way of saying "the file should exist".
Second, consider
open(unit=nhist,file=history,err=700)
700 ierr=-1
if (ierr /=0) then
...
If there's an error here, execution is transferred to the statement labelled 700. From this statement ierr is set to a non-zero value and off we go to the if construct to handle that error.
It's just that the statement labelled 700 also happens to be executed even without an error: it's simply the next statement after the open and there's no branch to miss it. [I could give an example of such branching, but I don't want to encourage use of err= in modern code. With the working iostat= things are far preferable.]
But if you just wish to test the existence of a file, consider inquire-by-file:
logical itexists
inquire (file=history, exist=itexists)
if (.not.itexists) error stop "No file :("
Some would argue this is even better than having status='old' in an open statement.

Write error in Fortran

I am struggling to read from one input text file into a new text file using the Fortran compiler in Visual Studio 10. My input file is in gslib format (attached)
program main
! Unit numbers:
lisa = 4
lfke = 9
! Writing input data to file Lisa
open(unit=lisa,file='fake.dat',status='OLD',err=80)
open(unit=lfke,file='dlfke',status='NEW', err=80)
read(lisa,*,err=80)
read(lisa,*,err=80) line
write(9,*) line
close(unit=lisa)
close(unit=lfke)
80 stop 'ERROR in test file!'
end
This program is always going to produce the error message "ERROR in test file", whether or not there is an error. After executing line the close statements, it will next execute line 80 with the stop statement. You will probably find it easier, as your develop your program, to remove the err=80 tests in each IO statement. Then if there is an IO error, the program will automatically terminate and produce a specific message about the program. These err=branches are useful if you want to have your program handle the error, but with this implementation its hiding what the IO error was since all IO errors produce the same message.

Error on line 50: attempt to give DATA in type-declaration

I am try to compile FORTRAN 77 code and I have problems like this.
integer row(nnzmax+nszero),column(nnzmax+nszero),
+ ireg(nximax),florsm(nzimax)/nzimax*2/
real lambda,imodel(nximax,nzimax),dm(nmmax),
+ dum1(nmmax),dum2(nmmax),data(ndmax+nsconst),
+ anz(nnzmax+nszero),ibmodel(nximax,nzimax),
+ smwz(nzimax)/nzimax*-1./,spwz(nzimax)/nzimax*-1./
Error on line 50: attempt to give DATA in type-declaration
Error on line 52: attempt to give DATA in type-declaration
I used to work with this code,but it has been compiled with Intel Fotran Compiler. I have moved to other country so I do not have ifort installed here. I am using fort77 now. Should I try with some compilation options or?I have used this script to compile app .f from this folder.
#! /bin/csh -f
set list=`ls *.f`
set FLAG="-o"
echo $list
foreach file (${list})
echo $file
f77 ${file} ${FLAG} ${file:r}
mv ${file:r} ../bin/.
end
I have changed declarations like this:
integer row(nnzmax+nszero),column(nnzmax+nszero),
+ ireg(nximax),florsm(nzimax),
+ data florsm /nzimax*2/
real lambda,imodel(nximax,nzimax),dm(nmmax),
+ dum1(nmmax),dum2(nmmax),data(ndmax+nsconst),
+ anz(nnzmax+nszero),ibmodel(nximax,nzimax),
+ data smwz /nzimax*-1./,
+ data spwz /nzimax*-1./
But still I got
Error on line 50: attempt to give DATA in type-declaration
Error on line 53: attempt to give DATA in type-declaration
Error on line 385: Declaration error for smwz: used as variable
Error on line 385: Declaration error for smwz: may not appear in namelist
Error on line 385: Declaration error for spwz: used as variable
Error on line 385: Declaration error for spwz: may not appear in namelist
This fragment, and the later similar ones
florsm(nzimax)/nzimax*2/
looks like a non-standard way of initialising a variable with a sort-of data statement merged into the declaration. A more standard approach would separate the two, something like
florsm(nzimax)
...
data florsm /nzimax*2/
One of the beauties of working with the Intel Fortran compiler is its long history; along the way it has picked up, and continues to accept, all sorts of non-standard features. I'm guessing that this is one of those and is not acceptable to the other compiler you mention.
Of course, this seems to be what the error statement seems to be telling us.
A standard replacement might be
florsm(nzimax) = 2
but that takes advantage of a Fortran 90 feature which something called fort77 might not understand either.

problem using formatted Fortran `write(6,*)` output

I'm currently porting an application from Fortran to C and need to output some variables to compare results. I'm very new to Fortran, and although i understand the code and have now ported several thousand lines, I'm a noob at writing Fortran code myself.
This code:
write(6,'(A,I3,A,E12.8,A,E12.8,A,E12.8,A,E12.8,A,E12.8)') 'iHyd:',
& ih,'; dzdr: ',dzdr,'; tauray:', tauRay,'; zRay: ',
& zray,'; ampRay: ',realpart(aray),'+j*',
& imagpart(aray),'; qRay: ',qray,'; width :',w
Compiles fine, but when run, the program exits with:
At line 296 of file calcpr.for (unit = 6, file = 'stdout')
Fortran runtime error: Expected INTEGER for item 15 in formatted transfer, got REAL
(A,I3,A,E12.8,A,E12.8,A,E12.8,A,E12.8,A,E12.8)
^
q0: 1432.3944878270595
nArrayR: 501 nArrayZ: 201
iHyd: 1; dzdr: ************; tauray:************; zRay: ************; ampRay: NaN+j* NaN
; qRay:
Besides being really ugly, it doesn't make much sense to me, as ìh is declared as integer*8 and not as real.
So how can i solve this?
I'm counting 6 character&variable specifications in the format statement, but you're printing 8 of them.
edit:
a nicer use of the format statement would be '(A,I3,7(A,E12.8))'
Fortran "recycles" the format if there are more things to be printed than specified in the format statement. If a write statement gives results you don't understand, to diagonose the problem it may be helpful to remove the things printed one at a time until the error goes away.
It says "item 15", which I would take to be down near the end of your list, not ih at the beginning. It's clear that both "w" and "qray" are being printed as REAL; is either one of them an INTEGER? You may need to change the format specifier then.