保存文件位置并在 Fortran 90 中返回

发布于 2024-12-16 21:14:12 字数 1028 浏览 3 评论 0原文

我正在编辑 fortran 90 来读取文件。一个特定的文件碰巧被一些额外的信息“污染”,所以我想尝试读取然后倒回;如果错误,请重读:

open(filenum,file=filename,form="unformatted",iostat=ierr) //'direct' access
...lots of stuff...
here = ftell(filenum)
read(filenum,iostat=ierr) var1, var2             //try reading as var1, var2
if(iswrong(var1, var2)) then                     //check if correct
  call fseek(filenum,here-ftell(filenum),1)      //rewind
  read(filenum,iostat=ierr) vara, varb, varc     //read as different type 
endif

但是,当我编译此程序时,我得到

Undefined symbols:
  "_fseek_", referenced from:
      ___myreader__subroutine_name in myreader.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

(我正在尝试在 gfortran (i686-apple-darwin8-gfortran-4.2) 上编译)。据我了解,fseek 不是标准的 Fortran 例程。

我想知道是否还有其他选择。我知道我可以做类似 read(filenum,rec=somevalue) 的事情,但是我如何以类似的方式使用它?我还考虑尝试使用 read(,advance='no') 进行读取以进行测试,然后使用 advance='yes' 再次读取(如果格式正确),但这需要特定的格式表达,这个没有指定。谢谢。

I am editing a fortran 90 to read a file. A particular file happens to be 'contaminated' with some extra information, so I wanted to attempt a read and then rewind; reread if in error:

open(filenum,file=filename,form="unformatted",iostat=ierr) //'direct' access
...lots of stuff...
here = ftell(filenum)
read(filenum,iostat=ierr) var1, var2             //try reading as var1, var2
if(iswrong(var1, var2)) then                     //check if correct
  call fseek(filenum,here-ftell(filenum),1)      //rewind
  read(filenum,iostat=ierr) vara, varb, varc     //read as different type 
endif

However, when I compile this program, I get

Undefined symbols:
  "_fseek_", referenced from:
      ___myreader__subroutine_name in myreader.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

(I am trying to compile on gfortran (i686-apple-darwin8-gfortran-4.2)). I understand that fseek is not a standard fortran routine.

I wonder if there is an alternative. I understand I can do something like read(filenum,rec=somevalue) but how can I use this in a similar fashion? I also thought to attempt reading with read(,advance='no') for testing and then reading again with advance='yes' if it is in the correct format, but this requires a specific format expression, which is not specified. Thank you.

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

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

发布评论

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

评论(3

错爱 2024-12-23 21:14:12

您必须使用退格键返回到上一条记录。传统的 Fortran IO 是基于记录的,而不是基于流的(如 C 语言)

You have to use backspace to go back to the previous record. Traditional Fortran IO is record based, non stream based (like in C)

摇划花蜜的午后 2024-12-23 21:14:12

在 Linux 和 Windows 上,通常计算寻道偏移量是可以的。

然而,在面向记录的文件系统(OpenVMS、OS/370、NOS 等)上,来自 ftell() 的值是一个神奇的 cookie,无法使用简单的算术来检查或处理计算新的文件位置。相反,请使用 rewind 语句返回到文件的开头(如果确实可查找)。

On Linux and Windows, it is okay in general to compute seek offsets.

However, on record-oriented file systems (OpenVMS, OS/370, NOS, etc.), the value which comes from ftell() is a magic cookie and cannot be inspected or processed using trivial arithmetic to compute a new file location. Instead, use the rewind statement to go back to the beginning of a file (if it is indeed seekable).

慕烟庭风 2024-12-23 21:14:12

如果您不确定文件内容(在这种情况下有时文件内容不正确或不同),还有另一种处理输入的技术,即读入字符串,然后检查字符串的内容。如果内容有效,您可以使用读取语句来读取字符串。或者,如果内容“不同”,请使用不同的读取语句,或采取适当的操作。无需倒带或退格。您必须猜测最长的可能行长度才能声明字符串的长度。

There is another technique for processing input if you are uncertain about the file contents -- in this case sometimes the files contents are incorrect or different -- which is to read into a string, then examine the contents of the string. If the contents are valid you can use a read statement to read from the string. Or if the contents are "different", use a different read statement, or take appropriate action. No rewind or backspace is necessary. You do have to guess the longest possible line length to declare the length of the string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文