Silverfrost Forums

Welcome to our forums

Logical Error with READ

8 Jan 2009 7:23 #4175

I'm trying to read 4 values from a file into 4 REAL arrays.

4 FORMAT(7F12.3) DO 20 I=3,11 JJ = I-1 READ(ICR,4) a(I), b(I), c(JJ), d(JJ) 20 CONTINUE

However, more than one element in the array is being updated at a time (or during one iteration through the loop). For instance, a(I)=file_value and a(I-2)=0. For each iteration, the (I-2) element is being assigned 0. It's really funky behavior that I can't figure out. Does anyone know the possible cause?

update: a(I-2) = d(JJ) AND d(JJ+2) = a(I) weird, why is the READ statement swapping these values?

9 Jan 2009 11:11 #4178

Not enough code in there for anyone to help. Where are the arrays declared? What sort of info is in the file?

Eddie

9 Jan 2009 12:37 #4179

I agree with Eddie, however, what the first iteration of this program will do is to read four values from the first line of the file, and it will only do this correctly if the data in the file is arranged:

  1. in columns of 12 characters wide, or
  2. each column is less than 12 characters and is separated with a comma and has decimal points present

When working out formats, I find it useful to use F for a fractional column,I for integer, X for space etc, and if two similar formats are adjacent, then alternate the case. For example, the format expects 7F12.3 looks like with some examples under it:

ffffffff.fffFFFFFFFF.FFFffffffff.fffFFFFFFFF.FFFffffffff.fffFFFFFFFF.FFFffffffff.fff
  9               25           40            123
9,25,40,123
9.,25.,40.,123.

For the first example, you might expect results of 9, 25, 40 & 123, but you will probably get 90000, 25, 4 & 0.123 The second example overrides the spacing, but on reading pushes everthing right to the edge of each format and the result will be 0.009, 0.025, 0.040 & 0.123. The third example also overidded the position of the decimal place and you will get the correct input. On input, it is better to use a wide format with zero decimal places, like 4F20.0, and override it with commas, the decimal places for whole numbers are not then required, but supplying a decimal place will also work for fractional values.

That said, the program only reads four values from each line as each sequential read starts at the start of a new line.

If the data in your file was for example, using values of the form 'row.column' for easy identification of the results.

1.1, 1.2, 1.3, 1.4 2.1, 2.2, 2.3, 2.4 3.1, 3.2, 3.3, 3.4 . . . . . 9.1, 9.2, 9.3, 9.4

Then the arrays would be filled thus:

I    A(I)   B(I)   C(I)   D(I)
 1   undef  undef  undef  undef
 2   undef  undef  1.3    1.4
 3   1.1    1.2    2.3    2.4
 4   2.1    2.2    3.3    3.4
 5   3.1    3.2    4.3    4.4
 6   4.1    4.2    5.3    5.4
 7   5.1    5.2    6.3    6.4
 8   6.1    6.2    7.3    7.4
 9   7.1    7.2    8.3    8.4
10   8.1    8.2    9.3    9.4
11   9.1    9.2    undef  undef

The dimensions of A, B, C & D are assumed to be A(11), B(11), C(11) & D(11) or greater.

I hope this helps.

Regards

Ian PS, forgot to say, instead of a format number in the read, use an asterisk.

13 Jan 2009 12:39 #4191

I found the problem. Thank you.

Please login to reply.