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

How to put comments in data file ?

forcpp
New Contributor I
3,393 Views
Hi all,
I want to know how to put comment(s) in data file that should be ignored during read command. I read some where by putting hash in starting of a comment it can be done. But when I follow this procedure, I get error.

For example:
program file: test.f90
program test
implicit none
integer :: i
real(8) :: x
!
open(1,file='data.d')
do i = 1, 5, 1
read(1,*)x
print*,x
end do
!
end program test

Data file: data.d
1
# ignore me
2
3
4
5
Error: 1.00000000000000
At line 8 of file test.f90
Fortran runtime error: Bad real number in item 1 of list input

Thanx.
0 Kudos
4 Replies
jimdempseyatthecove
Honored Contributor III
3,393 Views

Read file lineinto a character variable, test first chare for '#', if '#' cycle, if not perform internal read of character variable into real. (to do an internal read substitute the character variable for the i/o unit).

Jim Dempsey
0 Kudos
forcpp
New Contributor I
3,393 Views

Read file lineinto a character variable, test first chare for '#', if '#' cycle, if not perform internal read of character variable into real. (to do an internal read substitute the character variable for the i/o unit).

Jim Dempsey

Thanx Jim. It mean without internal read we can not ignore commented line in spite of we defined the variable type.

Thanks again for giving your valuable time.

0 Kudos
jimdempseyatthecove
Honored Contributor III
3,393 Views

The other route to take is to place the comment on the same line

1 ! one potato
2 ! two carrots
3 ! three peas
...

Then let the read pull in the number and discard the remainder of the record.
When reading one (or fixed number of) variable(s) a comment can be anything following a field seperator

1one potato
2two carrots
3three peas

But you may want to generalize the input and permit expressions

1+2/3 cups milk

Or have your input file be pseudo language with variables

milk=1+2/3 cups
potato=1
carrots=2
peas=3

In the above "cups" is a volumetric scaling factor (oz, quarts, etc).

There are scripting languages that you can incorporate into your input routine. My preference is to roll my own.

Another technique I use is to make the input file in a form usable by the Fortran Preprocessor (or C Preprocessor). Edit that file but add to make file or solution a "compilation" through FPP

[cpp]! GRUN77.h

#include "GTOSS.inc"
! The above include file has
! #define LateStart nn
! #define AVRDBSnapshotInterval mm
! #define DELTAT tt
! ... and a whole bunch of additional code values
!       
C When "compiling" for late start uncomment the following define
! #define LATE_START
...
#ifdef LATE_START
LateStart  1.0                 ! 1.0 TO REQUEST LATE START
AVRDBSnapshotInterval   360.0  ! AVRDB Snapshot Interval (seconds)
DELTAT                  0.05   ! DELTAT: REF PT
#else
DELTAT                  0.05   ! DELTAT: REF PT
! DELTAT                  0.01 ! DELTAT: REF PT
!AVRDBSnapshotInterval   360.0 ! AVRDB Snapshot Interval (seconds)
AVRDBSnapshotInterval   720.0  ! AVRDB Snapshot Interval (seconds)
#endif
[/cpp]


The FPP technique permits you to maintain a readable input file with comments, limited variables, and conditional compilation plus include files.

To compile

FPP -P -noC grun77.h grun77.dat

I use a batch file

! GRUN.BAT
FPP -P -noC GRUN%1.h GRUN%1.dat


And then issue

grun nn

where nn is the sequence number of the test data. You could use

! h2dat
FPP -P -noC %1.h %1.dat

Have fun

Jim Dempsey
0 Kudos
forcpp
New Contributor I
3,393 Views

The other route to take is to place the comment on the same line

1 ! one potato
2 ! two carrots
3 ! three peas
...

Then let the read pull in the number and discard the remainder of the record.
When reading one (or fixed number of) variable(s) a comment can be anything following a field seperator

1one potato
2two carrots
3three peas

But you may want to generalize the input and permit expressions

1+2/3 cups milk

Or have your input file be pseudo language with variables

milk=1+2/3 cups
potato=1
carrots=2
peas=3

In the above "cups" is a volumetric scaling factor (oz, quarts, etc).

There are scripting languages that you can incorporate into your input routine. My preference is to roll my own.

Another technique I use is to make the input file in a form usable by the Fortran Preprocessor (or C Preprocessor). Edit that file but add to make file or solution a "compilation" through FPP

[cpp]! GRUN77.h

#include "GTOSS.inc"
! The above include file has
! #define LateStart nn
! #define AVRDBSnapshotInterval mm
! #define DELTAT tt
! ... and a whole bunch of additional code values
!       
C When "compiling" for late start uncomment the following define
! #define LATE_START
...
#ifdef LATE_START
LateStart  1.0                 ! 1.0 TO REQUEST LATE START
AVRDBSnapshotInterval   360.0  ! AVRDB Snapshot Interval (seconds)
DELTAT                  0.05   ! DELTAT: REF PT
#else
DELTAT                  0.05   ! DELTAT: REF PT
! DELTAT                  0.01 ! DELTAT: REF PT
!AVRDBSnapshotInterval   360.0 ! AVRDB Snapshot Interval (seconds)
AVRDBSnapshotInterval   720.0  ! AVRDB Snapshot Interval (seconds)
#endif
[/cpp]


The FPP technique permits you to maintain a readable input file with comments, limited variables, and conditional compilation plus include files.

To compile

FPP -P -noC grun77.h grun77.dat

I use a batch file

! GRUN.BAT
FPP -P -noC GRUN%1.h GRUN%1.dat


And then issue

grun nn

where nn is the sequence number of the test data. You could use

! h2dat
FPP -P -noC %1.h %1.dat

Have fun

Jim Dempsey
Thanks again for the alternate suggestion.
0 Kudos
Reply