Reading data file in Fortran with known number of lines but unknown number of entries in each line

前端 未结 5 943
孤街浪徒
孤街浪徒 2020-12-02 01:24

How can I read the data file containing known number of lines but the number of entries in each line is unknown, e.g. if my data file contain some thing like

5条回答
  •  误落风尘
    2020-12-02 01:46

    This is a program that can count numbers in a line (or number of columns) but for a line. If you have many lines, you should change it slightly.

    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
    

提交回复
热议问题