FORTRAN95 dealing with space delimited data files

In summary, the programmer is trying to read in a 20x20 matrix of integers using FORTRAN 95, but is having difficulty because the default format of '*' reads the first column and skips over the space delimited data on the same row. A better solution would be to use a labeled FORMAT statement in the READ statement.
  • #1
JesseC
251
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
  • #2
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
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
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
Thanks for the advice, I'll play around with the READ and FORMAT statements.
 

Related to FORTRAN95 dealing with space delimited data files

1. What is FORTRAN95?

FORTRAN95 (short for Formula Translation 95) is a high-level programming language commonly used in scientific and engineering applications. It was developed in the 1950s and has since undergone several revisions, with FORTRAN95 being the most recent standard.

2. How do I read space delimited data files in FORTRAN95?

To read space delimited data files in FORTRAN95, you can use the built-in READ statement with the format specifier "FMT='(A)'". This will read each line of the file as a character string, which you can then parse and convert into the appropriate data type using the internal READ and CONVERT functions.

3. Can FORTRAN95 handle large data files?

Yes, FORTRAN95 can handle large data files as it has no inherent limitations on file size. However, the amount of memory available on your computer may affect its ability to process large files.

4. How do I write data to a space delimited file in FORTRAN95?

To write data to a space delimited file in FORTRAN95, you can use the WRITE statement with the format specifier "FMT='(A)'". This will write the data as a character string, with spaces between each value. You can also use the internal WRITE and CONVERT functions to format the data before writing it to the file.

5. Is FORTRAN95 suitable for processing data from space missions?

Yes, FORTRAN95 is suitable for processing data from space missions as it is a highly efficient and reliable programming language. It has been used in various space missions, including the Apollo program and the Hubble Space Telescope. Additionally, FORTRAN95 has features that make it well-suited for scientific and engineering applications, such as its ability to handle complex mathematical operations and large datasets.

Similar threads

  • Programming and Computer Science
Replies
4
Views
634
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top