|
[Sponsors] |
September 12, 2006, 10:06 |
read in line / fortran
|
#1 |
Guest
Posts: n/a
|
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 |
|
September 12, 2006, 10:59 |
Re: read in line / fortran
|
#2 |
Guest
Posts: n/a
|
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 |
|
September 12, 2006, 11:56 |
Re: read in line / fortran
|
#3 |
Guest
Posts: n/a
|
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. |
|
September 13, 2006, 05:50 |
Re: read in line / fortran
|
#4 |
Guest
Posts: n/a
|
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.
|
|
September 13, 2006, 10:30 |
Re: read in line / fortran
|
#5 |
Guest
Posts: n/a
|
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 |
|
September 15, 2006, 10:41 |
Re: read in line / fortran
|
#6 |
Guest
Posts: n/a
|
Thanks a lot everybody.
You have answered my question very well. |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Comparison between C/C++ and Fortran? | rick | Main CFD Forum | 45 | September 6, 2011 01:52 |
CFX11 + Fortran compiler ? | Mohan | CFX | 20 | March 30, 2011 19:56 |
How to read files in parallel using fortran and mpi | quarkz | Main CFD Forum | 0 | November 21, 2010 11:41 |
Installation OF1.5-dev | ttdtud | OpenFOAM Installation | 46 | May 5, 2009 03:32 |
[blockMesh] Axisymmetrical mesh | Rasmus Gjesing (Gjesing) | OpenFOAM Meshing & Mesh Conversion | 10 | April 2, 2007 15:00 |