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

How to read tab delimited, variable width values with scientific notation from text file

jirina
New Contributor I
1,306 Views

I would like to read read values with scientific notation from a text file. Values are delimited by tab and they can have different width, e.g. (tab is replaced by space in the example below).

 -1.15e+02 -2.156e+01 -3.1e+00 1.4256e+01

Is there a simple way of specifying such a format that would allow reading the text file in the following way?

OPEN ( UNIT=unitFile, FILE=filePathName, ACTION='READ', ACCESS='SEQUENTIAL', FORM='FORMATTED' )
do i=iMin,iMax
  READ ( unitFile, '(a1)', ADVANCE='NO' ) charTab
  READ ( unitFile, '(???)', ADVANCE='NO' ) valueDouble
enddo

I would like to avoid having to parse each line in the text file. If parsing cannot be avoided in this case, how could I obtain a value represented by a substring of a line?

0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
1,306 Views

I suggest you READ each entire line into a suitably sized CHARACTER variable,
Replace each TAB with ',' (comma). IOW 'TABTAB ' is replaced with ',,' to permit handling of missing values.

Then use the edited CHARACTER variable in place of the unitFile in the READ statement. IOW perform the formatted/list directed READ using the edited CHARACTER variable for input.

Jim Dempsey

0 Kudos
jirina
New Contributor I
1,306 Views

Thank you for your suggestion; I am going to give it a try if I do not succeed convincing my colleagues to change the format to e.g. fw.d.

0 Kudos
jirina
New Contributor I
1,306 Views

I should have mentioned that I know how many rows and columns there are in the file. This allows me to read the whole line:

real*8, allocatable, dimension(:,:) :: field2D

allocate ( field2D(Nrows,Ncolumns) )

do i=1,Nrows
  READ ( unitFile, * ) field2D(i,:)
enddo

I tested this and it works well, no matter if the file contains values in E format or F format (I did not try a mixture of both formats though).

0 Kudos
Reply