How to read data from a file with different column in a line

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to read data from a file with different column in a line

1838409

Dear Sir/Madam;
I want to read data from a file in which every line has different  
number of data, e.g.

a b c d
e f
h g k

and I need to have format like this format(T8,<num>(I7)) which <num>  
is dynamic format specifier determined in run time which specified  
number of column in a row of input file. This is the code I want to  
modified. The code worked in Intel fortran, but in gfortran I got error.

do i=1,N
      read(20,'(I7)') CV(i,1)

      backspace(20)
      15 format(T8,CV(i,1)(I7))
      read(20,15) CV(i,2:CV(i,1)+1)
end do

I'd really appreciate it if you could help me.

Regards,
Mehdi Baba Mehdi


Reply | Threaded
Open this post in threaded view
|

Re: How to read data from a file with different column in a line

Arjen Markus-4
Op vr 26 jul. 2019 om 08:50 schreef <[hidden email]>:

>
>
> Dear Sir/Madam;
> I want to read data from a file in which every line has different
> number of data, e.g.
>
> a b c d
> e f
> h g k
>
> and I need to have format like this format(T8,<num>(I7)) which <num>
> is dynamic format specifier determined in run time which specified
> number of column in a row of input file. This is the code I want to
> modified. The code worked in Intel fortran, but in gfortran I got error.
>
Note: this type of usage question is better asked on
comp.lang.fortran. But to give you a clue:
You were using an extension of the Intel Fortran compiler. The
standard way to achieve this,
is to construct the format string via an internal write. Other
solutions are possible too - just use
a large repeat count (there is a limit to the repeat count by the way).

Regards,

Arjen
Reply | Threaded
Open this post in threaded view
|

Re: How to read data from a file with different column in a line

Thomas Koenig-6
In reply to this post by 1838409
Am 26.07.19 um 08:50 schrieb [hidden email]:

>
> Dear Sir/Madam;
> I want to read data from a file in which every line has different number
> of data, e.g.
>
> a b c d
> e f
> h g k
>
> and I need to have format like this format(T8,<num>(I7)) which <num> is
> dynamic format specifier determined in run time which specified number
> of column in a row of input file.

 From the 9.1 docs:

6.2.2 Variable FORMAT expressions

A variable FORMAT expression is format statement which includes angle
brackets enclosing a Fortran expression: FORMAT(I<N>). GNU Fortran does
not support this legacy extension. The effect of variable format
expressions can be reproduced by using the more powerful (and standard)
combination of internal output and string formats. For example, replace
a code fragment like this:

       WRITE(6,20) INT1
  20   FORMAT(I<N+1>)

with the following:

c     Variable declaration
       CHARACTER(LEN=20) FMT
c
c     Other code here...
c
       WRITE(FMT,'("(I", I0, ")")') N+1
       WRITE(6,FMT) INT1

or with:

c     Variable declaration
       CHARACTER(LEN=20) FMT
c
c     Other code here...
c
       WRITE(FMT,*) N+1
       WRITE(6,"(I" // ADJUSTL(FMT) // ")") INT1


HTH

        Thomas