Read format question

This is a discussion on Read format question within the Fortran forums in Programming Languages category; This should be simple, but I can't get it to work. I have a data file consisting of a number of lines of data, each line containing an integer then either nothing or a second bit of data which can be real or an integer, eg: 23 114 2 115 4356.45 I have a bit of code that is supposed to read this, which does not work: do while (.not. eof(iun)) read(iun, *, iostat=ierr) iii if(ierr == 0) then if(iii == 114) then read(iun, *, iostat=ierr) rdata idata = int(rdata) elseif(iii == 115) then read(iun, *, iostat=ierr) rdata endif endif ...

Go Back   Application Development Forum > Programming Languages > Fortran

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-22-2008, 06:04 AM
ferrad
Guest
 
Default Read format question

This should be simple, but I can't get it to work.
I have a data file consisting of a number of lines of data, each line
containing an integer then either nothing or a second bit of data
which can be real or an integer, eg:

23
114 2
115 4356.45

I have a bit of code that is supposed to read this, which does not
work:

do while (.not. eof(iun))
read(iun, *, iostat=ierr) iii
if(ierr == 0) then
if(iii == 114) then
read(iun, *, iostat=ierr) rdata
idata = int(rdata)
elseif(iii == 115) then
read(iun, *, iostat=ierr) rdata
endif
endif
enddo


What read / format statements can I use to get this to work?
Reply With Quote
  #2  
Old 08-22-2008, 06:52 AM
Arjen Markus
Guest
 
Default Re: Read format question

On 22 aug, 12:04, ferrad <ac...@hotmail.com> wrote:
> This should be simple, but I can't get it to work.
> I have a data file consisting of a number of lines of data, each line
> containing an integer then either nothing or a second bit of data
> which can be real or an integer, eg:
>
> 23
> 114 2
> 115 4356.45
>
> I have a bit of code that is supposed to read this, which does not
> work:
>
> * * * * do while (.not. eof(iun))
> * * * * * read(iun, *, iostat=ierr) iii
> * * * * * if(ierr == 0) then
> * * * * * * if(iii == 114) then
> * * * * * * * read(iun, *, iostat=ierr) rdata
> * * * * * * * idata = int(rdata)
> * * * * * * elseif(iii == 115) then
> * * * * * * * read(iun, *, iostat=ierr) rdata
> * * * * * * endif
> * * * * * endif
> * * * * enddo
>
> What read / format statements can I use to get this to work?


I would do it in this way:
- read the line in as a string (long enough for all data)
- then read from that line:
read( line, *, iostat = ierr ) iii, rdata
- check ierr: if not zero, then there is only one item
- do:
read( line, *, iostat = ierr ) iii
- check ierr: the line might be empty

Each read from the file will go to the next line in the file.
So you first store the line in a string variable and then
you can read it as often as needed.
(You can not use non-advancing I/O because the format does not
seem fixed.)

Regards,

Arjen
Reply With Quote
  #3  
Old 08-22-2008, 07:41 AM
Kurt Kallblad
Guest
 
Default Re: Read format question


"ferrad" <acfer@hotmail.com> wrote in message
news:6bc67016-cbdb-479c-9e7e-a6e9aca0c065@34g2000hsh.googlegroups.com...
> This should be simple, but I can't get it to work.
> I have a data file consisting of a number of lines of data,
> each line
> containing an integer then either nothing or a second bit of
> data
> which can be real or an integer, eg:
>
> 23
> 114 2
> 115 4356.45
>
> I have a bit of code that is supposed to read this, which does
> not
> work:
>
> do while (.not. eof(iun))
> read(iun, *, iostat=ierr) iii
> if(ierr == 0) then
> if(iii == 114) then
> read(iun, *, iostat=ierr) rdata
> idata = int(rdata)
> elseif(iii == 115) then
> read(iun, *, iostat=ierr) rdata
> endif
> endif
> enddo
>
>
> What read / format statements can I use to get this to work?


One problem with your code is that each list directed read (read
with * as format) reads a new line, thus when 114 has been read
next read statement will read 115.

If you use Fortran 90 or later following can be used.

character(len=32) :: buffer
...
do
read(iun, '(a)', iostat = i) buffer
if(i /= 0) exit
read(buffer, *) iii
if(iii == 114) then
read(buffer, *) iii, idata
elseif(iii == 115) then
read(buffer, *) iii, rdata
endif
enddo

In Fortran 77 it's more difficult as the list directed read is
not allowed for internal files (reading from a character string).
Hope you are using a modern Fortran.

Good luck,
Kurt

Reply With Quote
  #4  
Old 08-22-2008, 01:31 PM
Richard Maine
Guest
 
Default Re: Read format question

ferrad <acfer@hotmail.com> wrote:

> I have a bit of code that is supposed to read this, which does not
> work:


Others have noted the basic problem - that you seem to be assuming
non-advancing I/O, which you don't specify in the code, and which won't
work with list-directed formatting (The asterisks) anyway. The simplest
solution to that involves, as they noted, reading each line first into a
character variable.

Let me also note that the line

> do while (.not. eof(iun))


is not standard or particularly portable Fortran because of the use of
the nonstandard eof functiuon. There is no such function in the
standard, and it isn't a particularly widespread extension. I recall
seeing it as an extension in some f66 compilers before f77 provided
standard ways to test for end of file. And I've seen it in a few
compilers since then. But it is just asking for portability problems.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:39 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.