# FORTRAN95 dealing with space delimited data files

1. Jun 18, 2011

### JesseC

I'm doing project Euler ID11 (http://projecteuler.net/index.php?section=problems&id=11), and I thought I'd try this one in FORTRAN 95. The first problem in the project is finding a suitable way of reading in a 20x20 matrix of integers.

I'm doing it by put the 20x20 matrix into a separate file called data1.txt and thought I could have FORTRAN read in each data point like this:
Code (Text):

integer :: m(20,20), i, j
open(1001,file='data1.txt')
do i = 1, 20
do j = 1, 20
end do
end do

The problem I have is that it READ only reads the first column before reaching end of file and skips the space delimited data on the same row. I got around this by writing:
Code (Text):

integer :: m(20,20), i
open(1001,file='data1.txt')
do i = 1, 20
read(1001,*) m(i,1), m(i,2), m(i,3), m(i,4), m(i,5), m(i,6), m(i,7), &
m(i,8), m(i,9), m(i,10), m(i,11), m(i,12), m(i,13), m(i,14), m(i,15), &
m(i,16), m(i,17), m(i,18), m(i,19), m(i,20)
end do
rewind(1001); close(1001);

But this is a really cumbersome and ugly solution! Surely there must be a better way?

2. Jun 18, 2011

### SteamKing

Staff Emeritus
Instead of using the default format '*' in your READ statement, you can reference a labeled FORMAT statement. In the FORMAT statement, you can account for how the data is organized in the data file. In the corresponding READ statement, try using an implied DO construct. For example:

READ (1001, 2000) M (I, J) (J = 1, 20)

2000 FORMAT (20I5)

3. Jun 19, 2011

### gsal

After many years of F77, I have just started to program in Fortran 90...so, I now do not use GOTOs nor CONTINUEs nor labels...

...along the idea of not using labels, I have also stopped using FORMAT statements...I either put the format string straight into the READ statement or declared a CHARACTER variable if I need to re-use the format and then put the string into the READ...

...having said that, SteamKing has the right idea, you need to use 20I....but do not use 20I5 because this truly will make your reading column oriented and it is not the correct constant width in your file; if anything, it looks like it could be 3, but I cannot tell from here whether the left-most numbers have a space on the left of them or not...so, just use 20I, like this:

READ(1001, '(20I)' ) ( M(I,J), J=1,20 )

I have capitalized all Fortran, but when I write code, I never capitalize anything (other than a few camel case variable names) ...it just make the screen so much more crowded...

4. Jun 20, 2011

### SteamKing

Staff Emeritus
The Fortran I edit descriptor must include a width, so that I3 and I5 are valid examples, but a simple I will generate an error.

For the table which you wish to read (as referenced by the link in the OP) a better fix would be:

READ (1001, 2000) M (I, J) (J = 1, 20)

2000 FORMAT (20I3)

and the following READ would also be valid (without a FORMAT):

READ (1001, *) M (I, J) (J = 1, 20)

5. Jun 20, 2011

### JesseC

Thanks for the advice, I'll play around with the READ and FORMAT statements.