如何在Fortran中读取内容不以空格分隔的2D文件 [英] How can I read a 2D file which content is not separated by spaces in Fortran
问题描述
我在文件(number.txt)中存储了一个矩阵,如下所示:
I have a matrix stored in a file (number.txt), like this:
12323456
54254311
76534522
我如何在Fortran中读取此类矩阵,所以结果将是:
How can I read such matrix in Fortran, so the result would be:
1 2 3 2 3 4 5 6
5 4 2 5 4 3 1 1
7 6 5 3 4 5 2 2
使用awk分隔这些列并在Fortran中读取它非常容易.但是,我想知道是否可以仅使用Fortran来完成所有这些工作.完成后,我需要将该矩阵按其转置倍数.
It is very easy to separate these columns using awk and read it in Fortran. But, I would like to know if I can do all this using only Fortran. After I am done with I will need to multiple this matrix by its transpose.
推荐答案
Fortran格式的输入和输出基于字段.字段不需要用空格分隔.在这里,您可以将数据 12323456
视为宽度为8的单个字段或宽度为1的8个字段(或其他组合).
Fortran formatted input and output is based on fields. Fields are not required to be separated by spaces. Here you may consider the data 12323456
to be a single field of width 8 or 8 fields of width 1 (or other combinations).
如果要从宽度为1的整数字段中读取值,格式项 I1
将允许读取该值.如果您有八个宽度为1的整数字段彼此相邻, 8I1
将允许读取这些字段.
If you want to read a value from a integer field of width 1, the format item I1
will allow that value to be read. If you have eight integer fields of width 1 next to each other 8I1
will allow reading of those.
implicit none
character(8) :: input(3)=['12323456','54254311','76534522']
integer i, values(8)
do i=1,3
read(input(i),'(8I1)') values
print '(8(I1,:,1X))', values
end do
end
这篇关于如何在Fortran中读取内容不以空格分隔的2D文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!