| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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? |
|
#2
| |||
| |||
| 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 |
|
#3
| |||
| |||
| "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 |
|
#4
| |||
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.