Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29271 Discussions

How to read .dat files with different filenames, please?

Zetian_R_
Beginner
706 Views

Hi,

I am using fortran for coding. Now I am going to read more than 1000 data files separately, from the data file folder D\Datas\fortran

And the data filenames are like:

Gstep00001.dat  Gstep00002.dat  ... Gstep01230.dat

and the contents are like:

    -0.0004349421733      1.0583843781537
    -0.0004059937166      1.0583825316509
    -0.0003770452599      1.0583806851481
    -0.0003480968032      1.0583788386453
    -0.0003191483465      1.0583769921425
    -0.0002901998898      1.0583751456397
    -0.0002612514331      1.0583732991369
    -0.0002323029764      1.0583714526341
    -0.0002033545197      1.0583696061313
    -0.0001744060630      1.0583677596285

....

....

So how to accomplish this in fortran, please? Waiting for your help, thanks!

Sinserely

Ren

 

0 Kudos
1 Solution
Arjen_Markus
Honored Contributor II
706 Views

I will assume that the problem you are facing is opening these 1000 files one by one without having to put these names into the program itself.

One way to do this is to use a so-called internal write statement: you write to a character string instead of a file. Here is a small example:

character(len=20) :: filename

do i = 1,1230
    write( filename, '(a,i5.5,a)' ) 'Gstep', i, '.dat'
    open( 10, file = filename )
    ... read and process the data ...
    close( 10 )
enddo

Note the format "i5.5" - it causes the program to insert 0's before the actual number, so that 111 is written as 00111.

I will leave the reading and processing of the actual data to you.

View solution in original post

0 Kudos
2 Replies
Arjen_Markus
Honored Contributor II
707 Views

I will assume that the problem you are facing is opening these 1000 files one by one without having to put these names into the program itself.

One way to do this is to use a so-called internal write statement: you write to a character string instead of a file. Here is a small example:

character(len=20) :: filename

do i = 1,1230
    write( filename, '(a,i5.5,a)' ) 'Gstep', i, '.dat'
    open( 10, file = filename )
    ... read and process the data ...
    close( 10 )
enddo

Note the format "i5.5" - it causes the program to insert 0's before the actual number, so that 111 is written as 00111.

I will leave the reading and processing of the actual data to you.

0 Kudos
Zetian_R_
Beginner
706 Views

Thank you so much!

0 Kudos
Reply