|
From: Luka Djigas on 22 Apr 2008 23:42 Hello all ! files: CWxx.DAT where xx is the number which goes from 00 to xx and they look like this: 14 lines of rubbish, and unknown number of lines of data (below) 3.500000E-01 5.427289 4.700469 7.268204E-01 4.000000E-01 6.566324 5.225193 1.341131 4.500000E-01 6.944819 5.329013 1.615806 5.000000E-01 6.206076 4.943774 1.262302 5.500000E-01 5.158059 4.395154 7.629050E-01 --------------(this line is not in file, just to show this is the end) I'm trying to read the data so I can plot it. I've managed to come up with some kooky solution for determining whether the file exists and to read everything up to the data lines (see below, pay no attention to the comments, they're here purely for the purpose of self-confusion) but I'm having trouble finding a way in which I can read the data lines up to the file end, and determining how many lines I've read in that file. Is there some common way this is usually done ? I searched the archives, and found some examples. If I read it with read(1,*,end=...) I don't see how I would be able to read how many lines I've read. Please, any help on this would be appreciated. regards Luka character(8) file_name logical existence character(100) not_important_line dimension fn(25),cw(25),cw0(25),cwi(25) ! Provjera koji fileovi CWxx.DAT postoje, i koji je zadnji xx do 1 i=0,99 write(file_name,'("CW",i2.2,".DAT")')i inquire(file=file_name, exist=existence) if(.not.existence) then ! write(*,'(" File ",a8," ne postoji")')file_name else n_last_file=i end if 1 continue ! ---------------------------- do 2 i=0,n_last_file write(file_name,'("CW",i2.2,".DAT")')i open(unit=1, file=file_name, status='old') rewind(1) do 3 j=1,14 read(1,'(a100)')not_important_line 3 continue do 4 j1=1,25 read(1,*)fn(j1),cw(j1),cw0(j1),cwi(j1) 4 continue close(1) 2 continue end program
From: e p chandler on 23 Apr 2008 00:11 On Apr 22, 11:42 pm, Luka Djigas <ldigas@@gmail.com> wrote: > Hello all ! > > files: CWxx.DAT where xx is the number which goes from 00 to xx > > and they look like this: > 14 lines of rubbish, and unknown number of lines of data (below) > 3.500000E-01 5.427289 4.700469 7.268204E-01 > 4.000000E-01 6.566324 5.225193 1.341131 > 4.500000E-01 6.944819 5.329013 1.615806 > 5.000000E-01 6.206076 4.943774 1.262302 > 5.500000E-01 5.158059 4.395154 7.629050E-01 > --------------(this line is not in file, just to show this is the end) > > I'm trying to read the data so I can plot it. I've managed to come up > with some kooky solution for determining whether the file exists and > to read everything up to the data lines (see below, pay no attention > to the comments, they're here purely for the purpose of > self-confusion) but I'm having trouble finding a way in which I can > read the data lines up to the file end, and determining how many lines > I've read in that file. > > Is there some common way this is usually done ? I searched the > archives, and found some examples. If I read it with read(1,*,end=...) > I don't see how I would be able to read how many lines I've read. > > Please, any help on this would be appreciated. > > regards > Luka [code snipped] What's wrong with brute force? Read each file twice, once to count the number of records, then to read in the data. While counting records, read in as a string and discard. No expensive format conversion is done on pass 1. re-allocate the data array or create an automatic array in a subroutine with the needed size. -- e
From: Richard Maine on 23 Apr 2008 00:17 Luka Djigas <ldigas@@gmail.com> wrote: > I'm having trouble finding a way in which I can > read the data lines up to the file end, and determining how many lines > I've read in that file. > > Is there some common way this is usually done ? I searched the > archives, and found some examples. If I read it with read(1,*,end=...) > I don't see how I would be able to read how many lines I've read. Um... by counting them? Start a counter at zero and increment it for every successful read. I hope that I don't need to post an example of counting? :-( Well, that works for explicit formats anyway. A list-directed read will read as many lines as needed to fine enough fields for its I/O list. There is no way to tell how many lines it read. To the extent that you are confident that the data file will be formatted such that each list-directed read will read exactly one line, the same counting scheme works. If you are particularly concerned about the possible distinction between number of reads and the number of lines, then list-directed is not a good choice. -- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain
From: Terence on 23 Apr 2008 01:08 Dzien dobre, Luka. You can find which CWxx.DAT files exist and what size they are and so how mnay records in each by using the directory enquiry via a SYSTEM (DIR CW??.DAT> mydir) call and reading back the created file. If the records are fixed length you simple divide file size by record length, for each file. You can also find the length of any file via another service call (an API one). If the records are not fixed length you will have to read them and count them first as suggested above.
From: andrej.panjkov on 23 Apr 2008 01:16 On Apr 23, 1:42 pm, Luka Djigas <ldigas@@gmail.com> wrote: [...] > Is there some common way this is usually done ? I searched the > archives, and found some examples. If I read it with read(1,*,end=...) > I don't see how I would be able to read how many lines I've read. [...] > > do 4 j1=1,25 > read(1,*)fn(j1),cw(j1),cw0(j1),cwi(j1) > 4 continue [...] Yes, one way is use read(1,*,end=6) with the target statement 6 after the end of the loop. The variable j1 will tell you how many lines (including possibly incomplete lines) were read. I like to set iostat instead, and test that. ioEndFlag = .false. do while (.not. ioEndFlag ) read(1,*, iostat=ioStat )fn(j1),cw(j1),cw0(j1),cwi(j1) if( iostat > 0 ) ioErrFlag=.true. if( iostat < 0 ) ioEndFlag=.true. end do
|
Next
|
Last
Pages: 1 2 Prev: curious about 8 byte LOGICAL... Next: Interpretation of RECL resolution; help file in CVF |