Silverfrost Forums

Welcome to our forums

READ error

20 Aug 2008 10:13 #3720

Hi,

I´m opening a file with arrays in this way:

2008-08-13 00:00:00,20.86,20.86,18.56,30.32,30.1,29.54,22.45,36.5,0,16.95

I create a read statement that reads all except seconds, the format is this one:

500 FORMAT(f4.0,4(1x,f2.0),3x,10(f5.2))

The point is, when I PRINT an array, it looks like this: 2008.00 8.0000 13.0000 0.0000 0.0000 0.0000 20.8600 0.0000 20.8600 0.0000 18.5600 0.0000 30.3200 0.0000 30.1000

THere is '0.0000' between numbers that appears magicly!!!, I can´t short it out by myself, any suggestion is welcomed!! Please!!!

thanks best regards

20 Aug 2008 7:57 #3721

Try this:

      winapp
      integer*4 iyear,imonth,iday,ih,im,is
      real*4 data(10)
      open(unit=10,file='data.txt',status='old')
!line in data.txt contains:
!2008-08-13 00:00:00,20.86,20.86,18.56,30.32,30.1,29.54,22.45,36.5,0,16.95
      read(10,500)iyear,imonth,iday,ih,im,is,data
  500 format(i4,5(1x,i2),1x,10(f12.0))
      print *,iyear,imonth,iday,ih,im,is,data
      close(unit=10)
      end

First, the year, month, day, hours, minutes and seconds are really integer and in fixed format, hence the first integer part of the read format.

Reading these first items effectively positions the input pointer at the first comma, which is interpreted as being the end of the first REAL data item, so to overcome this, put in a 1x to align the free format (comma separated) read with the first fractional data item. I have then used larger F formats which appear to expect no decimal places. This can be important on some compilers, which use the actual alignment of the data and place it in the appropriate digit positions. This is however overwritten by the decimal point and the comma overrides the length of the characters being input. By putting f12.0, it allows up to 12 characters to be read and right aligns them in the field, resulting in a correct value if no decimal place is present. This was what I was taught to do back in 1973 on the ICL 1900 series computer and it still seems to work.

Regards

Ian

Please login to reply.