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

时间:2020-08-22 08:03:07

标签: arrays fortran


更新:请在下面查看对此帖子所做的编辑!为了符合讨论的逻辑,该帖子本身仍将是原始帖子!


我正在尝试编写一个测试程序,该程序打开一个用户指定的文件名-包含两个数据列和一行描述包含数据的行(在该行之后),因此它可能会生成两个具有该特定大小的数组,以读和写它。指定的文件结构如下所示。我一直在使用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”(我不希望在文件中进行硬编码!),并且具有以下结构,如前所述:第一行描述矩阵的行数,其余行是仅由空格分隔的数据列。

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

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


编辑#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@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

1 个答案:

答案 0 :(得分:0)

经过几次尝试和错误,并感谢这里提出的宝贵意见,使我能够使程序正常工作!我的粗心大意阻碍了这一尝试和错误-我没有检查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
相关问题