Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having trouble getting "&" to work. 2

Status
Not open for further replies.

corpdog

Programmer
Jul 27, 2011
4
0
0
US
So, I'm still pretty new to FORTRAN. I'm trying to test out the methods of creating and reading from external files using the "OPEN, READ, and WRITE" commands. However, I keep running into problems when I try to use the "&" symbol to link two lines.

I'm using gedit as my script (working in Ubuntu with gfortran as my compiler).

Here is my code:

PROGRAM reader2
!This program is used to test the OPEN and CLOSE functions
IMPLICIT NONE

INTEGER :: i_err
REAL :: i,j

OPEN(UNIT=30, FILE='info2.dat', STATUS='REPLACE', &
ACTION='WRITE', IOSTAT=i_err)

OPEN(UNIT=100, FILE ='info.dat',STATUS='OLD',ACTION='READ')

READ(100,*) i,j

WRITE(30,*) i
WRITE(30,*) j

END

And the error message that I keep getting is:

cory@cory-VirtualBox:~/Documents$ gfortran reader2.f
reader2.f:8.55:

OPEN(UNIT=30, FILE='info2.dat', STATUS='REPLACE', &
1
Error: Syntax error in OPEN statement at (1)
reader2.f:9.11:

ACTION='WRITE', IOSTAT=i_err)
1
Error: Unclassifiable statement at (1)

 
I tried it with gfortran and it works:
from input file
info.dat
Code:
2447893       0
I got output file
info2.dat
Code:
   2447893.0    
   0.0000000
 
What script program were you using. Also, did you format it any special way within the script?
 
I copied the program from your post - i.e. this
Code:
PROGRAM reader2
    !This program is used to test the OPEN and CLOSE functions
    IMPLICIT NONE

    INTEGER :: i_err
    REAL :: i,j

    OPEN(UNIT=30, FILE='info2.dat', STATUS='REPLACE', &
         ACTION='WRITE', IOSTAT=i_err)

    OPEN(UNIT=100, FILE ='info.dat',STATUS='OLD',ACTION='READ')
    
    READ(100,*) i,j

    WRITE(30,*) i
    WRITE(30,*) j
    
    END
It works fine with gfortran and g95
I have these versions
Code:
$ gfortran --version
GNU Fortran (GCC) 4.4.0 20080603 (experimental) [trunk revision 136333]
Copyright (C) 2008 Free Software Foundation, Inc.

GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU Fortran
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING

$ g95 --version
G95 (GCC 4.0.4 (g95 0.92!) Oct 14 2008)
Copyright (C) 2002-2005 Free Software Foundation, Inc.

G95 comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of G95
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING
 
That is bizarre; I see no reason why that continuation character wouldn't work. Maybe a stupid question, but what version of gfortran are you running? Execute "gfortran --version" and see what it says.

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
Nick,

it's probably a good question to ask. I was considering the same thing.

"GNU Fortran (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.

GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU Fortran
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING"

So, it is a different version than the one listed above. I wonder if that has anything to do with the error.
 
corplog said:
So, it is a different version than the one listed above. I wonder if that has anything to do with the error.
IMHO, it doesn't have to do with version.
It's feature of fortran compilers, that the compilation is depending of the [highlight]file extension[/highlight].
If I'm using reader2[highlight].f95[/highlight] then it compiles fine, but when I try to compile reader2.[highlight].f[/highlight] it doesn't compile:
Code:
$ gfortran reader2.f95 -o reader2

$ gfortran reader2.f -o reader2
...
Error: Unclassifiable statement at (1)
reader2.f:8.5:

    OPEN(UNIT=30, FILE='info2.dat', STATUS='REPLACE', &                 
    1
Error: Non-numeric character in statement label at (1)
reader2.f:8.5:

    OPEN(UNIT=30, FILE='info2.dat', STATUS='REPLACE', &                 
    1
Error: Unclassifiable statement at (1)
reader2.f:9.9:

         ACTION='WRITE', IOSTAT=i_err)                                  
        1
...
If the file extension is [highlight]*.f[/highlight] or [highlight]*.for[/highlight] then the compiler assumes that the language is legacy Fortran fixed-form.

Therefore:
either
rename your file from reader2[highlight].f[/highlight] to reader2[highlight].f95[/highlight]
or
use the compiler option [highlight]-ffree-form[/highlight] i.e.:
Code:
$ gfortran reader2.f -o reader2 -ffree-form
 
I think that is because a .f file is interpreted as a fortran77 file in which you need an & in the sixth column of the continuation line
 

I think that is because a .f file is interpreted as a fortran77 file in which you need an & in the sixth column of the continuation line

Read again the precise explanation provided by mikrom : fixed form and free form are not related to a Fortran version. The fixed form was the only form available before the arrival of Fortran-90 but may be used in a program using F2008 features like coarrays. So a file with the suffix .f may contain instructions which are not recognized by a Fortran-77 compiler.

Today, nobody should use a Fortran-77 compiler because it exists several free F95 compilers (g95, gfortran, silverfrost and intel (free only for linux)).



François Jacq
 
Thanks guys, I got it to work!

the "-ffree-form" fixed the problem
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top