CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Main CFD Forum (https://www.cfd-online.com/Forums/main/)
-   -   read in line / fortran (https://www.cfd-online.com/Forums/main/12205-read-line-fortran.html)

jojo September 12, 2006 09:06

read in line / fortran
 
Hi everybody,

I have a tricky question on Fortran.

What is the programming trick to read several numbers in line like:

123 234 345 456 789 678

I want to read the 'n' first numbers before going to the following line. Is it possible to control that in a n-loop? Basically there will be an outer loop based on the lines in the file (like usual) and an inner loop to control the number of columns read in each line.

Thanks a lot in advance.

jojo

Nico September 12, 2006 09:59

Re: read in line / fortran
 
This should work, 1000 being a number >n Write(1,'(1000i3)') (array(i),i=1,n) another way would be to concatenate n & 'i3' in a character.

Nico

Renato. September 12, 2006 10:56

Re: read in line / fortran
 
Well..., I would load the data in a buffer and store only what I want or read dummy arguments to make the read statement consistent for each line. Imagine a file with 10 lines, each line has 5 numbers and you like to store only the 1st., 3rd. and 5th. numbers, you could do something like:

do i=1,10 read(iin,*) n1, ntrash1, n3, ntrash2, n5 enddo

or

do i=1,10 read(iin,*) (ibuf(j),j=1,5) ! here you can select which value must be stored enddo

hope it helps you

Renato.

ps.: There's a format statement where you tell how many values must be read. I don't remember clearly, but it seems something like:

9 format(< n >i3)

here you can set "n" equal to the number of integer values that must be read.


HelpfulSoul September 13, 2006 04:50

Re: read in line / fortran
 
Neither of these solutions are particularly useful in the context of the stated problem as they both require that there be at least 5 numbers on every line rather than an unknown number - what happens if there are less (or more) than 5 ? This could be corrected using the IOSTAT to stop the program crashing out if there are too few numbers and making the required number of values very large compared to the expected number of values, but the implicit loop from the previous poster's solution is the way to go.


Gerrit September 13, 2006 09:30

Re: read in line / fortran
 
I doubt that I understood you well, but just in case...

DO iline=1,imxline,1

READ(1,*)(x(icolumn),icolumn=1,n,1) END DO

jojo September 15, 2006 09:41

Re: read in line / fortran
 
Thanks a lot everybody.

You have answered my question very well.


All times are GMT -4. The time now is 18:23.