Pergunta

I am using some old fortran code for a biology project I am doing. I am posting the relevant snippets here. Here is a subroutine called "READCN". Earllier in the program MAXN was set to 108.

    OPEN ( UNIT = CNUNIT, FILE = CNFILE,
 :         STATUS = 'OLD', FORM = 'UNFORMATTED' )
    READ ( CNUNIT ) N, BOX
    IF ( N .GT. MAXN ) STOP ' N TOO LARGE '
    READ ( CNUNIT ) ( RX(I), I = 1, N ), ( RY(I), I = 1, N )

    CLOSE ( UNIT = CNUNIT )

    RETURN
    END

I am inputting a file called "data.dat" to the program. Here is the file:

10, 4
0.8147,    0.1576
0.9058,    0.9706
0.1270,    0.9572
0.9134,    0.4854
0.6324,    0.8003
0.0975,    0.1419
0.2785,    0.4218
0.5469,    0.9157
0.9575,    0.7922
0.9649,    0.9595

Nevertheless, I always get the message "N TOO LARGE". Any advice? Thanks!

Foi útil?

Solução

Don't open as unformatted, it will read your file as if it were binary data. Open as formatted instead, and use "*" format. Also, don't read in one line, as you would not read your data in the expected order.

  program bob
  implicit none
  integer cnunit, n, maxn, box, i
  parameter(maxn=108, cnunit=10)
  real rx(maxn), ry(maxn)
  open(unit=cnunit, file='bob.txt', status='old', form='formatted')
  read(cnunit, *) n, box
  print *, 'n=', n, 'box=', box
  if(n .gt. maxn) stop 'n too large'

  do i=1, n
     read(cnunit, *) rx(i), ry(i)
     print *, rx(i), ry(i)
  end do

  close(unit=cnunit)
  end

Alternately, if you can't change the code, then change your input file to fit the needs of your program. The input file you give simply won't work: you need binary data, in the format expected by your compiler (there is the usual, non portable "record size"), and data must be given column-wise, not row-wise.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top