Results 1 to 4 of 4

Thread: Strange behaviour of elementary fortran code

  1. #1

    Default Strange behaviour of elementary fortran code

    Can anyone suggest what I am not seeing in regard to the following simple fortran program, which produces a bizarre outcome (I am running openSUSE 11.2 64-bit).

    !-------------------------------------------------------------
    ! junk.f90 18 Jan/2011

    program junk
    implicit none

    integer :: q

    open(2,file='junk.par',status='old')
    print*,'parameters from junk.par'

    open(6,file='./out/junk.out',status='unknown')
    !open(7,file='./out/morejunk.out',status='unknown')

    read(2,*) q
    print*,q

    stop
    end program junk
    !------------------------------------------------------------

    Compiled ("gfortran -o junk junk.f90") and run it produces:

    ~/flang/urbanLS>./junk
    parameters from junk.par
    ~/flang/urbanLS>

    and (this is what is odd) the integer "q" is written into file junk.out.

    However if I perturb the program to

    !open(6,file='./out/junk.out',status='unknown')
    open(7,file='./out/morejunk.out',status='unknown')

    and recompile, the result is (the expected behaviour):

    ~/flang/urbanLS>./junk
    parameters from junk.par
    1
    ~/flang/urbanLS>

    and there is no write to file morejunk.out. How to explain this!?! Thanks, jdw

  2. #2
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,851

    Default Re: Strange behaviour of elementary fortran code

    Please: Posting in Code Tags - A Guide.

    Maybe others can read it, but I have problems, thus at least for your next post that involves code.
    Henk van Velden

  3. #3

    Default Re: Strange behaviour of elementary fortran code

    Having looked at your code, that is the sort of behaviour I would expect from traditional Fortran. I am not sure if things have changed in the last few years but Fortran used to use certain pre-defined unit numbers for the standard input, output and error channels. In statements such as:

    'read(n,' etc
    'write(n,' etc)
    'open(n' etc
    'close(n' etc

    'n' here is the unit number. This is 5 for standard input, 6 for standard output and 0 for standard error. When you perform a 'print' statement that is equivalent to 'write(6,' etc

    Hence, what you did with your 'open(6,' etc statement is redirect the standard ouput to the file 'junk.out' for all 'print' and 'write(6,' etc statements following. When you ditched that and did an 'open(7,' etc, the standard ouput remained on unit 6 (normally the screen) and 'morejunk.out' only received output from any write(7, etc statements.

    Hope that helps.

  4. #4

    Default Re: Strange behaviour of elementary fortran code

    Thanks -- you hit the nail... I'd forgotten those conventions, having not rubbed noses with them for some fraction of a century! I am migrating programs that I'd written on a PC and had been compiling using an Intel compiler. I'm enjoying linux and gfortran etc., and had (hitherto) had the blind luck not to encounter this factor. Thanks again -- jdw

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •