New Reply

FORTRAN95 dealing with space delimited data files

 
Share Thread Thread Tools
Jun18-11, 06:43 AM   #1
 

FORTRAN95 dealing with space delimited data files


I'm doing project Euler ID11 (http://projecteuler.net/index.php?se...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?
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> King Richard III found in 'untidy lozenge-shaped grave'
>> Google Drive sports new view and scan enhancements
>> Researcher admits mistakes in stem cell study
Jun18-11, 08:58 AM   #2
 
Recognitions:
Homework Helper Homework Help
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)
Jun19-11, 04:43 PM   #3
 
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...
Jun20-11, 04:30 AM   #4
 
Recognitions:
Homework Helper Homework Help

FORTRAN95 dealing with space delimited data files


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)
Jun20-11, 07:59 AM   #5
 
Thanks for the advice, I'll play around with the READ and FORMAT statements.
New Reply
Thread Tools


Similar Threads for: FORTRAN95 dealing with space delimited data files
Thread Forum Replies
Matlab read in data files Math & Science Software 2
openning new files and write data into them Programming & Comp Sci 3
statistic data files for SPSS Biology, Chemistry & Other Homework 0
Opening up data files on C. Engineering, Comp Sci, & Technology Homework 8
Matlab - writing data to .xy files Math & Science Software 5