呼叫时错误

发布于 2025-02-12 04:23:23 字数 367 浏览 1 评论 0原文

试图打开一些文件时,应用程序会导致一些I/O错误。该应用程序很大,有数十个子例程&模块和跳过其中一个没有解决问题,因为另一个会导致类似的问题。我参加了其中一个子例程,添加了一些行(以获取额外的信息),重新编译并重新运行。结果是环境变量似乎缺失或错误。例如,当我尝试使用呼叫get_environment_variable('pwd',pwd',pwd)并打印出PWD的值(定义为contrafe> targine(len = 128)),我得到了错误fortran运行时错误:记录的结尾。我也尝试了CWD,但结果相同。似乎代码中的某些内容正在改变这些环境变量或使它们无法访问,但是我无法将其手指放在什么或如何查明原始问题上。

An app is causing some I/O errors when trying to open some files. The app is pretty large with dozens of subroutines & modules and skipping one of them doesn't fix the problem because another one will cause a similar one. I took one of the subroutines, added some lines (for extra info), recompiled and rerun it. The results is that the environmental variables seem to be missing or fault. For example, when I try to get the current directory with call get_environment_variable('PWD',pwd) and print out the value of pwd (defined as character(len=128)), I'm getting the error Fortran runtime error: End of record. I also tried with cwd but same result. It seems like something in the code is altering these environmental variables or making them inaccessible, but I cannot put my fingers on what is it or how to pinpoint the original issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泼猴你往哪里跑 2025-02-19 04:23:23

内存损坏可能会导致代码中任何地方的意外后果。除非您提供问题的“最低可再现例子”,否则将不可能给出正确的答案。

但是,关于您的错误消息:编译器告诉您您正在阅读的字符变量pwd太短(很容易是数百个字符)。

我建议您首先查询该字符变量的长度,然后使用分配字符串存储它并使用status检查一切都正确

integer :: char_length,ierr
character(:), allocatable :: pwd

! Get string length of variable PWD
call get_environment_variable('PWD',length=char_length)

if (char_length<=0) stop 'environment does not contain PWD variable'

! Allocate dynamic string
allocate(character(len=char_length) :: pwd)

! Get its value
call get_environment_variable('PWD',value=pwd,status=ierr)

! Check success
select case (ierr)
   case (0); ! all right, do nothing
   case (1); stop 'PWD does not exist'
   case (2); stop 'this compiler does not support environment variables'
end select

Memory corruption could cause unintended consequences anywhere in the code. It will be impossible to give a right answer unless you provide a "minimum reproducible example" of your problem.

Regarding your error message though: the compiler is telling you that the character variable you're reading PWD in is too short (could be easily hundreds of characters).

I suggest you first query the LENGTH of that character variable, then use an allocatable string to store it and use STATUS to check that everything went right

integer :: char_length,ierr
character(:), allocatable :: pwd

! Get string length of variable PWD
call get_environment_variable('PWD',length=char_length)

if (char_length<=0) stop 'environment does not contain PWD variable'

! Allocate dynamic string
allocate(character(len=char_length) :: pwd)

! Get its value
call get_environment_variable('PWD',value=pwd,status=ierr)

! Check success
select case (ierr)
   case (0); ! all right, do nothing
   case (1); stop 'PWD does not exist'
   case (2); stop 'this compiler does not support environment variables'
end select
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文