Fortran FORTRAN95 dealing with space delimited data files

AI Thread Summary
The discussion focuses on solving a problem in FORTRAN 95 related to reading a 20x20 matrix of integers from a file for Project Euler ID11. The initial approach involved reading each element individually, which was cumbersome. A more efficient solution was suggested using a FORMAT statement and an implied DO construct. Specifically, the recommended method is to use a single READ statement with a format like '20I' or '20I3', which accommodates the matrix structure without needing to specify each element separately. This approach simplifies the code and enhances readability. The conversation also touches on coding practices, such as avoiding the use of labels and opting for direct format strings in READ statements. Overall, the emphasis is on improving code efficiency and clarity when handling matrix input in FORTRAN.
JesseC
Messages
247
Reaction score
2
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:
integer :: m(20,20), i, j
open(1001,file='data1.txt')
do i = 1, 20
    do j = 1, 20
	    read(1001,*) m(i,j)
    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:
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?
 
Technology news on Phys.org
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)
 
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...
 
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)
 
Thanks for the advice, I'll play around with the READ and FORMAT statements.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
8
Views
1K
Replies
12
Views
3K
Replies
5
Views
5K
Replies
5
Views
2K
Replies
1
Views
3K
Replies
3
Views
2K
Back
Top