CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Main CFD Forum

reading from line no x in fortran

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old   May 19, 2006, 02:31
Default reading from line no x in fortran
  #1
zonexo
Guest
 
Posts: n/a
hi,

may i know if it is possible to start reading in an ascii file from line no. x e.g line 4 in fortran?

hope someone can help. tks!
  Reply With Quote

Old   May 19, 2006, 08:51
Default Re: reading from line no x in fortran
  #2
ag
Guest
 
Posts: n/a
You'll need to code up something to skip the first n-1 lines with dummy reads to read the nth line in. Something like

char*1 skip

do i = 1, n-1

read (iunit,*) skip end do

read (iunit,*) data you want
  Reply With Quote

Old   May 19, 2006, 11:42
Default tks, what abt reading data on the same line?
  #3
zonexo
Guest
 
Posts: n/a
thanks ag,

that's just what i've been doing. ;-)

btw, i've data which are situated on the same line, separated by space.

i tried to read by:

do i=1,n read (1,fmt=*) u(i) end do

however using the above, i can only read in the 1st data of that line. moreover, different rows 've different no. of data. hence, it is not possible to use

read (1,fmt=*) u(i),v(i)...

and lastly, how abt if the data are arranged in this fashion:

1.12345678e1231.12345678e12312....

ie there are no space bet 2 nos although i know their length is fixed ie 1.12345678e123

thanks in advance!
  Reply With Quote

Old   May 19, 2006, 13:20
Default Re: tks, what abt reading data on the same line?
  #4
pc
Guest
 
Posts: n/a
You can try guessing the # of variables and back up if an error is encountered. For example:

nvars=10 readin=.false. do while(.not.readin) read(iunit,*,err=5)(u(i),i=1,nvars) readin=.true. cycle 5 nvars=nvars-1 backspace(iunit) enddo

This may not be the most elegant code but it should get the job done.

In response to your second question, if you know the field width you can use a formatted read. For example,

read(iunit,5)(u(i),i=1,10) 5 format(10(1pe14.8))

  Reply With Quote

Old   May 19, 2006, 13:23
Default Re: tks, what abt reading data on the same line?
  #5
pc
Guest
 
Posts: n/a
Sorry about the formatting, let's try that again:

nvars=10

readin=.false.

do while(.not.readin)

read(iunit,*,err=5)(u(i),i=1,nvars)

readin=.true.

cycle

5 nvars=nvars-1

backspace(iunit)

enddo

  Reply With Quote

Old   May 19, 2006, 17:18
Default Re: tks, what abt reading data on the same line?
  #6
Ananda Himansu
Guest
 
Posts: n/a
First Question regarding skipping first n lines of sequential formatted file to start reading on n+1'th line:

do i = 1, n

read(1,*)

end do

If the file is unformatted, the read statement above would be replaced with read(1)

Second Question regarding reading numbers separated with blanks but random number of numbers per line. "pc" has a good solution. In the special case where the numbers are all of the same type, and you know the total number "m" of numbers, simply use:

read(1,*) (u(i), i = 1, m)

Third Question regarding packed numbers with no separator. "pc" has the right answer.
  Reply With Quote

Old   May 20, 2006, 11:23
Default Re: tks, what abt reading data on the same line?
  #7
zonexo
Guest
 
Posts: n/a
tks alot everyone. i'll try them a try!
  Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Gmsh nishant_hull Open Source Meshers: Gmsh, Netgen, CGNS, ... 23 August 5, 2015 02:09
OpenFOAM 1.7.1 installation problem on OpenSUSE 11.3 flakid OpenFOAM Installation 16 December 28, 2010 09:48
Regarding FoamX running Kindly help out hariya03 OpenFOAM Pre-Processing 0 April 18, 2008 04:26
Axisymmetrical mesh Rasmus Gjesing (Gjesing) OpenFOAM Native Meshers: blockMesh 10 April 2, 2007 14:00
error while compiling the USER Sub routine CFD user CFX 3 November 25, 2002 16:16


All times are GMT -4. The time now is 06:27.