如何在FORTRAN中读取结构未知的数据文件?
我需要读取包含未定义数量的单元格的文件,并且对于每个单元格未知数量的数据对。我正在使用 Fortran。
该文件看起来像这样:
Cell Number 1
Depth1 Volume1
Depth2 Volume2
Depth3 Volume3
.
.
.
Cell Number N
Depth1 Volume1
Depth2 Volume2
Depth3 Volume3
Depth4 Volume4
Depth5 Volume5
有人可以帮助我吗?
I need to read files that contain an undefined number of cells and for each cell an unknown number of data pairs. I am using Fortran.
The file looks like that:
Cell Number 1
Depth1 Volume1
Depth2 Volume2
Depth3 Volume3
.
.
.
Cell Number N
Depth1 Volume1
Depth2 Volume2
Depth3 Volume3
Depth4 Volume4
Depth5 Volume5
Can somebody help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如@yosukesabia 所回答的,您可能想要使用标准方法读取字符串,然后查看字符串来决定您刚刚读取的内容,然后基于该方法从字符串中进行内部读取。
但还有一个问题是读取数据后如何存储......
由于单元格数量未知,每个单元格数据项数量未知,因此最优雅的方法是为每个单元格创建一个链接列表类型。拥有一个单元格列表,当遇到新单元格时,将其添加到该列表中。并且单元格类型本身包含单元格数据类型列表的列表。当您遇到新的数据项时,您可以将其添加到该列表中。链表可能是处理未知数量项目的最佳方法。最近的一个问题与 Fortran 中的链接列表有关:如何在 fortran 2003-2008 中实现链表。否则,您可以读取文件,倒带或退格,并分配正确大小的数组,然后重新读取。原始方法是使用固定长度的类型数组,其大小为最大可能的单元数以及每个单元的最大可能数据项数。简单,但非常不优雅。如果您对最大数量的猜测是错误的,则容易出现错误。
As answered by @yosukesabia, you probably want to use the standard approach of reading into a string, and then looking at the string to decide what you just read, then based on that doing an internal read from the string.
But there is also the issue of how to store the data once you have read it ...
Since you have both an unknown number of cells and an unknown number of data items per cell, the most elegant approach would be to create a linked list type for each. To have a list of cells, and when you encounter a new cell, add to that list. And the cell type itself contain a list of cell-data type list. When you encounter a new data item, you add to that list. A linked list is probably the best way to handle an unknown number of items. A recent question pertained to linked lists in Fortran: How can I implement a linked list in fortran 2003-2008. Otherwise you could read the file, rewind or backspace, and allocate arrays of the correct size, then re-read. The primitive way is to have fixed length arrays of the types, sized at the maximum possible number of cells, and the maximum possible number of data items per cell. Simple, but very inelegant. And bug prone if your guess of the maximum number is wrong.
您使用的是哪个版本的 Fortran? 95?
作为一个字符(len = 1000)或足够长的东西读入,然后从该变量中读取。
which version of fortran are you using? 95?
read in as a character(len=1000) or something long enough, and then read from that variable.