简体   繁体   English

如何使用while循环读取fortran中的文件?

[英]How to read a file in fortran by using a while loop?

I am trying to read a text file using a Fortran code.我正在尝试使用 Fortran 代码读取文本文件。 I have a file with 1999 rows and the number of columns vary with each row.我有一个包含 1999 行的文件,每行的列数都不同。 Can someone please tell me how one can code such a problem.有人可以告诉我如何编码这样的问题。 This is my code for reading a 4*2 text file but I am using do loops which I can't use in my current case.这是我读取 4*2 文本文件的代码,但我正在使用 do 循环,但在当前情况下无法使用。

PROGRAM myread2
IMPLICIT NONE

  INTEGER, DIMENSION(100) :: a, b
  INTEGER :: row,col,max_rows,max_cols

  OPEN(UNIT=11, file='text.txt')

  DO row = 1,4
    READ(11,*) a(row), b(row)
  END DO
  PRINT *, a(1)
  PRINT *, a(4)
  PRINT*, b(4)
END PROGRAM myread2

The best way of reading a file like this depends on how you want to store the data.读取此类文件的最佳方式取决于您希望如何存储数据。 I'm going to use a ragged array as it's probably simplest, although other container types may be better suited depending on your requirements.我将使用不规则数组,因为它可能是最简单的,但其他容器类型可能更适合您的要求。

Fortran doesn't have ragged arrays natively, so first you need to define a type to hold each row. Fortran 本身没有参差不齐的数组,所以首先你需要定义一个类型来保存每一行。 This can be done as这可以作为

type :: RowData
  integer, allocatable :: cols(:)
end type

type(RowData), allocatable :: rows(:)

When this container is filled out, the value in the i 'th column of the j 'th row will be accessed as当这个容器被填满时,第j行的第i列中的值将被访问为

value = rows(j)%cols(i)

We can then write a program to read the file, eg然后我们可以编写一个程序来读取文件,例如

type :: RowData
  integer, allocatable :: cols(:)
end type

type(RowData), allocatable :: rows(:)

integer :: no_rows
integer :: no_cols
integer :: i

open(unit=11, file='text.txt')

no_rows = count_rows(11)
allocate(rows(no_rows))

do i=1,no_rows
  no_cols = count_cols(11)
  allocate(rows(i)%cols(no_cols))
  
  read(11,*) rows(i)%cols
enddo

Now we just need to write the functions count_rows , which counts the number of rows in the file, and count_cols , which counts the number of columns on a given line of the file.现在我们只需要编写函数count_rows ,它计算文件中的行数,和count_cols ,它计算文件给定行上的列数。

Following this question , count_rows can be written as这个问题之后count_rows可以写成

! Takes a file unit, and returns the number of lines in the file.
! N.B. the file must be at the start of the file.
function count_lines(file_unit) result(output)
  integer, intent(in) :: file_unit
  integer :: output
  
  integer :: iostat
  
  output = 0
  iostat = 0
  do while (iostat==0)
    read(file_unit, *, iostat=iostat)
    if (iostat==0) then
      output = output+1
    endif
  enddo
  rewind(file_unit)
end procedure

And, similarly, count_cols can be written as并且,类似地, count_cols可以写为

! Takes a file unit, and returns the number of integers on the current line of the file.
function count_cols(file_unit) result(output)
  integer, intent(in) :: file_unit
  integer :: output
  
  integer :: iostat
  
  output = 0
  iostat = 0
  
  do while(iostat==0)
    read(file_unit, '(I0)', iostat=iostat, advance='no')
    if (iostat==0) then
      output = output+1
    endif
  enddo
  backspace(file_unit)
end function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM