读取数据文件Fortran中,在每个行已知数量的线,但条目的数量不详

问题描述 投票:1回答:5

我怎样才能读取包含已知数量的线,但条目中的每一行是未知的,例如数的数据文件如果我的数据文件包含像一些事情

1 3 4 5 6 -7 8 -9

1 3 5 6

4 5 6 7 8 3 5 6 7 8 4 5 7 8

即三行但在每行中的数据是未知的。有一次我从一条线所需要的数据。

fortran
5个回答
8
投票

一个方法:读出的线成一个字符串,使用一个字符串,它是至少只要最长预期线。然后你去解析字符串。例如,如果数字总是用空格分开,用它来计算出子边界。然后你可以使用“内部写着”从每个子串来读取获得的数值。内部读取使用字符串,而不是一个单位数和从字符串获得数据 - 至少你不必重新创建人物到数值转换,读语句为你做的。提供的Fortran内部函数将会使解析更容易。


3
投票

基于什么的实现M. S. B.指出。已经很晚了,但我想它可能是有用的人。

你有没有想到阅读准备的类型的数组:

double precision, dimension(MAX_NUM_OF_COLS) :: test_array

阅读您的文件一行:

READ(reading_unit,'(A)',iostat=io) line

环路,并尝试从行改为数中的最大数量:

do i=1,MAX_NUM_OF_COLS
  READ(line, *, iostat=io) test_array(1:i)
  if(io==0) exit
enddo

write(*,*) 'number of columns = ', (i-1)

如果需要的话,这个循环在你的文件的所有线路,并保持最大或列的最小数量。

最小例如:

integer, parameter :: MAX_NUM_OF_COLS=30
integer, parameter :: MAX_LINE_LENGTH=1000
character(len=MAX_LINE_LENGTH) line
integer i, io, reading_unit
double precision, dimension(MAX_NUM_OF_COLS) :: test_array

reading_unit=100
OPEN(reading_unit, file='the_file')

! Get first line of file.
DO
  READ(reading_unit,'(A)',iostat=io) line
  IF (io/=0) then
    write(*,*) "Error reading file."
    stop
  endif
  exit ! Eventually, do not exit and put the DO loop below here.
ENDDO
CLOSE(reading_unit)

do i=1,MAX_NUM_OF_COLS
  READ(line,*,iostat=io) test_array(1:i)
  if(io==-1) exit
enddo

write(*,*) 'number of columns = ', (i-1)

2
投票

假设你没事带补零(具体指的是后来复制问题here)的阵列,这是我的想法:

读取线的数据线转换成字符串,然后附加多个零,最后从该阵列中读取数据的每一行。下面是一个例子:

program unknown_numbers

    implicit none
    integer, parameter :: nrow=3, ncol=14
    integer :: data(ncol, nrow)
    character(len=2*ncol) :: zeros ! will contain lots of zeros
    character(len=10*ncol) :: line ! temporary storage for each line of the file
    integer :: i, u

    ! Write " 0 0 0 0 0 0 0 0" into the string "zeros"
    write(zeros, '(*(I2))') [(0, i=1, ncol)]

    open(newunit=u, file='data.txt', status='old', action='read')

    do i = 1, nrow, 1
        ! Read the next line into a temporary string array
        read(u, '(A)') line
        ! Append a number of zeros to the temporary string
        line = trim(line) // zeros
        ! Read the row of data from the string.
        read(line, *) data(:, i)
    end do

    close(u)

    ! For testing purposes, print the data.
    print '(14(X, I3))', data

end program unknown_numbers

1
投票
integer,parameter :: reclen=99999        ! maximum record length
integer,parameter :: undef=-9999         ! undefined value
integer :: actual_reclen                 ! actual record length
integer,dimension(reclen) :: dummy=undef ! dummy array used for reading
integer,dimension(:),allocatable :: a    ! final array

open(unit=10,file='sample.txt',form='formatted',access='sequential')
read(unit=10,fmt=*,end=101)(dummy(i),i=1,reclen)
101 close(unit=10)

actual_reclen=COUNT(dummy/=undef)
allocate(a(actual_reclen))
a=dummy(1:actual_reclen)

end

-3
投票

这是一个程序,它可以在一个线(或列数),但对于一个行计数数字。如果你有很多行,你应该稍微改变它。

program test12

implicit none

integer n,m,i

integer,allocatable::x(:)

 open(10,file='C:\Users\user\Desktop\file.txt')

allocate(x(n))

20 n=n+1

 deallocate(x) 



 allocate(x(n))

read(10,*,iostat=m)(x(i),i=1,n)

if (m==-1)then

goto 30

else


rewind 10

 goto 20

end if

 30 print*,n-1

 end
© www.soinside.com 2019 - 2024. All rights reserved.