首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Fortran编译灾难-变量数组大小

Fortran编译灾难-变量数组大小
EN

Stack Overflow用户
提问于 2020-08-22 08:03:07
回答 1查看 110关注 0票数 0

更新:请看下面的编辑这篇文章!为了与讨论的逻辑保持一致,帖子本身将保持原来的状态!

我正在编写一个测试程序,它打开一个用户指定的文件名,其中包含两个数据列,并有一行描述有多少行(该行后面)包含数据,因此它可能会生成两个具有特定大小的数组来读取和写入数据。下面的答案描述了指定的文件结构。我一直在使用布局here的思想来创建可变大小数组:

代码语言:javascript
运行
复制
    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”(我不希望在该文件中硬编码!)并具有如下结构,如前所述:第一行描述矩阵的行数,其余的行是仅由空格分隔的数据列。

代码语言:javascript
运行
复制
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

在编译过程中,编译器会产生一些非常不寻常的错误,我不理解这些错误。

代码语言:javascript
运行
复制
 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重新编译,它编译得很好。但是,在执行该文件时,我收到以下错误消息:

代码语言:javascript
运行
复制
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的建议修改了源代码。现在应改为如下所示。

代码语言:javascript
运行
复制
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

编译错误消失了,但是在程序执行过程中出现了一个错误:

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-22 10:34:31

经过几次尝试和错误,并感谢宝贵的意见在这里,我能够使程序正确工作!这个试验和错误被我的粗心大意所阻碍--我没有检查matrix.dat,尽管有人告诉我它可能被覆盖了--而且它已经被覆盖了!后置问题是由于文件格式错误而不是程序本身造成的。这是清理过的密码!谢谢大家!

代码语言:javascript
运行
复制
  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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63533909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档