Fortran 编译灾难 - 可变数组大小

Fortran Compilation disaster - variable array size


更新:请参阅下面对此 post 所做的编辑! post为了保持讨论的逻辑,还是保持原来的样子!


我正在尝试编写一个测试程序来打开用户指定的文件名 - 包含两个数据列和一行描述有多少行(在该行之后)包含数据,因此它可能会生成两个数组大小来读写它。此答案的下方描述了指定的文件结构。我一直在使用 here 提出的想法来创建可变大小的数组:

    program char
    implicit none
    character(len = 200)  :: filename;
    integer :: line_number;


    print*,'What is the file name?'
    read(*,*) filename
    print*, 'the file is: ',filename
    open(1,file=filename, status = 'old')
    read(1,*)line_number
    call read_data(line_number)
    end program char

     subroutine read_data(N)
     implicit none
     integer, intent (in) :: N;
     integer :: imax, i;
     real,dimension(N) :: a,b;

     imax=N+1
     do i=2,imax
      read(1,*)a(i),b(i)
      write(1,*)a(i),b(i)
     end do

     end subroutine read_data

我的测试文件名为 'matrix.dat'(我不希望在文件中进行硬编码!)并且具有以下结构,如前所述:第一行描述矩阵的行数,其余行是仅由 space.

分隔的数据列
9
1.0 1.1
2.0 2.2
3.0 3.3
4.0 4.4
5.0 5.5
6.0 6.6
7.0 7.7
8.0 8.8
9.0 9.9

在编译过程中,编译器产生了一些非常不寻常的错误,我不明白。

 Strelok@Yggdrasil:~$ gcc char.f95 -o char.exe
/usr/bin/ld: /tmp/cciriF9x.o: in function `read_data_':
char.f95:(.text+0x105): undefined reference to `_gfortran_st_read'
/usr/bin/ld: char.f95:(.text+0x135): undefined reference to `_gfortran_transfer_real'
/usr/bin/ld: char.f95:(.text+0x165): undefined reference to `_gfortran_transfer_real'
/usr/bin/ld: char.f95:(.text+0x174): undefined reference to `_gfortran_st_read_done'
/usr/bin/ld: char.f95:(.text+0x1af): undefined reference to `_gfortran_st_write'
/usr/bin/ld: char.f95:(.text+0x1df): undefined reference to `_gfortran_transfer_real_write'
/usr/bin/ld: char.f95:(.text+0x20f): undefined reference to `_gfortran_transfer_real_write'
/usr/bin/ld: char.f95:(.text+0x21e): undefined reference to `_gfortran_st_write_done'
/usr/bin/ld: /tmp/cciriF9x.o: in function `MAIN__':
char.f95:(.text+0x295): undefined reference to `_gfortran_st_write'
/usr/bin/ld: char.f95:(.text+0x2b0): undefined reference to `_gfortran_transfer_character_write'
/usr/bin/ld: char.f95:(.text+0x2bf): undefined reference to `_gfortran_st_write_done'
/usr/bin/ld: char.f95:(.text+0x2fa): undefined reference to `_gfortran_st_read'
/usr/bin/ld: char.f95:(.text+0x318): undefined reference to `_gfortran_transfer_character'
/usr/bin/ld: char.f95:(.text+0x327): undefined reference to `_gfortran_st_read_done'
/usr/bin/ld: char.f95:(.text+0x362): undefined reference to `_gfortran_st_write'
/usr/bin/ld: char.f95:(.text+0x37d): undefined reference to `_gfortran_transfer_character_write'
/usr/bin/ld: char.f95:(.text+0x39b): undefined reference to `_gfortran_transfer_character_write'
/usr/bin/ld: char.f95:(.text+0x3aa): undefined reference to `_gfortran_st_write_done'
/usr/bin/ld: char.f95:(.text+0x421): undefined reference to `_gfortran_st_open'
/usr/bin/ld: char.f95:(.text+0x45c): undefined reference to `_gfortran_st_read'
/usr/bin/ld: char.f95:(.text+0x47a): undefined reference to `_gfortran_transfer_integer'
/usr/bin/ld: char.f95:(.text+0x489): undefined reference to `_gfortran_st_read_done'
/usr/bin/ld: /tmp/cciriF9x.o: in function `main':
char.f95:(.text+0x4bb): undefined reference to `_gfortran_set_args'
/usr/bin/ld: char.f95:(.text+0x4cc): undefined reference to `_gfortran_set_options'
collect2: error: ld returned 1 exit status

如有任何帮助,我们将不胜感激。另外,如果有人能为我提供一种让程序自动评估列大小而不是让我声明它的方法 a priori,我将不胜感激!


EDIT #1: 按照@Swift - Friday Pie的建议,我使用gfortran重新编译,编译正常。 然而,在执行文件时,我收到以下错误消息:


strelok@Yggdrasil:~$ gfortran char.f95 -o char.exe
strelok@Yggdrasil:~$ ./char.exe
What is the file name?
matrix.dat
the file is: matrix.dat                                                                                                                                                                                          
At line 23 of file char.f95 (unit = 1, file = 'matrix.dat')
Fortran runtime error: End of file

Error termination. Backtrace:
#0  0x7faccc5c0d0a
#1  0x7faccc5c1819
#2  0x7faccc5c24ef
#3  0x7faccc802b3b
#4  0x7faccc7fbcf6
#5  0x7faccc7fcc99
#6  0x55f77a465341
#7  0x55f77a4656a4
#8  0x55f77a4656dd
#9  0x7faccc3d60b2
#10  0x55f77a46514d
#11  0xffffffffffffffff

编辑 #2:源代码已修改,如 @albert and @Ian Bush 所建议。它现在应该如下所示。

program char
implicit none
character(len = 200)  :: filename;
integer :: line_number;


print*,'What is the file name?'
read(*,*) filename
print*, 'the file is: ',filename
open(10,file=filename, status = 'old')
read(10,*)line_number
call read_data(line_number)
end program char

 subroutine read_data(N)
 implicit none
 integer, intent (in) :: N;
 integer :: imax, i;
 real,dimension(N) :: a,b;
 read(10,*)
 do i=1,N
  read(10,*) a(i),b(i)
  write(*,*) a(i),b(i)
 end do

 end subroutine read_data

编译错误没有了,但是在程序执行过程中,出现错误:

strelok@Yggdrasil:~$ ./char.exe
 What is the file name?
matrix.dat
 the file is: matrix.dat                                                                                                                                                                                          
   1.00000000       1.10000002
At line 22 of file char_original.f95 (unit = 10, file = 'matrix.dat')
Fortran runtime error: End of file

Error termination. Backtrace:
#0  0x7f714b51dd0a
#1  0x7f714b51e819
#2  0x7f714b51f4ef
#3  0x7f714b75fb3b
#4  0x7f714b758cf6
#5  0x7f714b759c99
#6  0x55bb2ff56382
#7  0x55bb2ff566e5
#8  0x55bb2ff5671e
#9  0x7f714b3330b2
#10  0x55bb2ff5614d
#11  0xffffffffffffffff

经过多次尝试和错误,感谢这里提出的宝贵意见,我才能够使程序正常运行!我的粗心阻碍了这次试验和错误 - 我没有检查 matrix.dat ,尽管被告知它可能被覆盖 - 它已经!后面的问题是由于文件格式错误造成的,而不是程序本身造成的。这是清理后的代码!谢谢大家!

  program char
  implicit none
  character(len = 200)  :: filename
  integer :: line_number

   print*,'What is the file name?'
   read(*,*) filename
   print*, 'the file is: ',filename
   open(10,file=filename, status = 'old')
   read(10,*)line_number
   call read_data(line_number)
  end program char

  subroutine read_data(N)
  implicit none
  integer, intent (in) :: N
  integer :: i, ios
  real,dimension(N) :: a,b

   do i=1,N
    read(10,'(2f3.2)',iostat=ios) a(i),b(i)
    if (ios /=0) print*,'Reading ERROR!'
    write(*,*) a(i),b(i)
   end do
  end subroutine read_data