如何在Fortran中将数据写入文件中的特定行?
我有无序数据,我想将此数据写入有顺序的文件中。例如,该值的顺序是第 70 行,则将该值写入文件中的第 70 行。如何向前移动文件指针?
有没有像BACKSPACE
这样的命令?
I have unordered data and I want to write this data to a file with an order. For example, the value's order is 70th, then this value is written to 70th line in the file. How can I move file pointer forward?
Is there any command like BACKSPACE
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您谈论行和退格键时,大概您的意思是 access="sequential" 和 form="formatted"。
在这种情况下,为了向前跳过记录,您可以只进行空读取,例如
As you're talking about lines and backspace, presumably you mean access="sequential" and form="formatted".
In that case, in order to skip a record forward, you can do just an empty read, e.g.
您还可以使用直接访问文件,其中可以不按顺序写入和读取记录。例如,请参见 http://en.wikipedia.org/wiki/Fortran_95_language_features#Direct- access_files
一天后编辑:
已建议使用顺序文件的解决方案。我认为这些不会起作用......如果您知道如何使其起作用,请解释一下。 (当然,你可以对内存中的值进行排序,然后按顺序写出。)下面是一些示例代码来说明问题。它创建一个 10 行的文件,然后假设您要写入第 5 个值:
输出文件包含:
顺序文件的问题是写入现有文件会擦除该点之后的所有内容。
与直接访问解决方案相比:
更短并且有效 - 输出文件包含十个数字,其中第五个数字从 5 更改为 99。但是,它们是二进制的。
You can also use a direct access file, in which the records can be written and read out of order. See, for example, http://en.wikipedia.org/wiki/Fortran_95_language_features#Direct-access_files
Edit after one day:
Solutions using a sequential file have been suggested. I don't think these will work ... please explain if you know how to make it work. (Of course, you can sort the values in memory and write them out sequentially.) Here is some sample code to illustrate the problem. It creates a file of 10 lines, then supposes that you want to write the 5th value:
The output file contains:
The problem for the sequential file is that a write to an existing file erases everything after that point.
Compare to the direct access solution:
Shorter and it works -- the output file contains ten numbers with the 5th changed from 5 to 99. However, they are binary.
对于每个数据条目,使用 janneb 描述的方法到达所需的行。然后使用 REWIND 语句返回到文件的开头(仅限 access='sequential')。
另外,如果您需要它,请查找格式描述符以了解如何沿单行向左/向右移动。
For each data entry, use method described by janneb to get to a desired line. Then use REWIND statement to go back to the beginning of the file (access='sequential' only).
Also in case you need it, look up format descriptors to see how to move left/right along a single line.