Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening several files

Status
Not open for further replies.

cealess

Technical User
Oct 24, 2007
13
CA
Hi everyone.
I am trying to open several files at once however it is not working and i cannot figure out why. I am writing out several files too and it's working. I'm getting the error that the files cannot be found (29). Thanks for the help

Program open_da
implicit none
Integer :: x,j,z
Character(len=10) :: file1, file2
character(len=2) :: input(j) = (/ '60', '61', '62', '63'... /)
character(len=4) :: output(j) = (/ '1960', '1961', '1962', '63'... /)

opendo: do x=1,j


file2=input(x)//'.txt'
OPEN (UNIT=3, FILE= file2, STATUS='OLD', IOSTAT=status)
openif: if(status==0) then

read(3,10, IOSTAT=status) (day(z), month(z), value(z), z=1,y)
else openif
write(*,1040) status
end if openif
10 FORMAT (I2,A3,F10.4)
1040 format (' ', 'Error opening the file: IOSTAT=', I6)

file1 = output(x)//'.txt'
OPEN(UNIT=1,FILE=file1,STATUS='new',FORM='FORMATTED')

!!! The rest of the program
end do opendo
end program open_da
 
You probably need to initialize j as a parameter somewhere before the declarations.

Try printing file2 after constructing it. Also set status to -1 before opening. If you're getting -1 back then open isn't filling in the status and you have to find out why.
 
Thaks. I figured out. It was a problem with the name of the files.

Now, i am having another problem which is when it's reading the files is keeping and writing values from the previous file. For example for the year 66 is writing some values from the year 65 in the output and it keeps doing this for the rest of the files.
 
Can't really tell unless you post some code. From the code you originally posted, you won't get that problem.
 
That is right.
Here is the code
program y_m5

Implicit none

! Variables
Integer :: i,j,z,aa,ab,ac,status,x,y
Integer :: jancount, febcount,marcount,aprcount, maycount, juncount, julcount, augcount, sepcount, octcount, novcount, deccount,jmax
Integer, Parameter :: nd=5000
Real, Dimension(nd) :: value
Real, Dimension(10000000) :: line
Real, Dimension(200) :: janu, febr, marc, apri, mayo, june, july, augu, sept, octo, nove, dece
Character(len=3), Dimension(nd) :: month
Integer, Dimension(nd) :: day
Character(len=20) :: file2
Character(len=20) :: file1
Real :: sumjan, sumfeb, summar, sumapr, summay, sumjun, sumjul, sumaug, sumsep, sumoct, sumnov, sumdec
Real :: janave, febave, marave, aprave, mayave, junave, julave, augave, sepave, octave, novave, decave
Character(len=3) :: input(47) = (/ '60', '61', '62', '63', '64', '65', '66', '67', '68', '69',&
'70', '71', '72', '73', '74', '75', '76', '77', '78', '79',&
'80', '81', '82', '83', '84', '85', '86', '87', '88', '89',&
'90', '91', '92', '93', '94', '95', '96', '97', '98', '99',&
'00', '01', '02', '03', '04', '05', '06' /)

Character(len=4) :: output(47) = (/ '1960', '1961', '1962', '1963', '1964', '1965', '1966', '1967', '1968', '1969',&
'1970', '1971', '1972', '1973', '1974', '1975', '1976', '1977', '1978', '1979',&
'1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987', '1988', '1989',&
'1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999',&
'2000', '2001', '2002', '2003', '2004', '2005', '2006' /)


! Body of y_m

opendo: do x=1,47

file2=input(x)//'.txt'

OPEN (UNIT=3, FILE=file2, STATUS='OLD', IOSTAT=status)

file1 = output(x)//'.txt'
OPEN(UNIT=1,FILE=file1,STATUS='new',FORM='FORMATTED')
openif: if(status==0) then


read(3,*, IOSTAT=status) (day(z), month(z), value(z), z=1,nd)
else openif
write(*,1040) status
end if openif

1040 format (' ', 'Error opening the file: IOSTAT=', I6)


jancount = 0
febcount = 0
marcount = 0
aprcount = 0
maycount = 0
juncount = 0
julcount = 0
augcount = 0
sepcount = 0
octcount = 0
novcount = 0
deccount = 0

do j=1, nd
if (month(j) .eq. 'Jan' ) then
jancount = jancount + 1
janu(jancount) = value(j)
else if (month(j) .eq. 'Feb') then
febcount = febcount + 1
febr(febcount) = value(j)
else if (month(j) .eq. 'Mar') then
marcount = marcount + 1
marc(marcount) = value(j)
else if (month(j) .eq. 'Apr') then
aprcount = aprcount + 1
apri(aprcount) = value(j)
else if (month(j) .eq. 'May') then
maycount = maycount + 1
mayo(maycount) = value(j)
else if (month(j) .eq. 'Jun') then
juncount = juncount + 1
june(juncount) = value(j)
else if (month(j) .eq. 'Jul') then
julcount = julcount + 1
july(julcount) = value(j)
else if (month(j) .eq. 'Aug') then
augcount = augcount + 1
augu(augcount) = value(j)
else if (month(j) .eq. 'Sep') then
sepcount = sepcount + 1
sept(sepcount)=value(j)
else if (month(j) .eq. 'Oct') then
octcount = octcount + 1
octo(octcount) = value(j)
else if (month(j) .eq. 'Nov') then
novcount = novcount + 1
nove(novcount) = value(j)
else if (month(j) .eq. 'Dec') then
deccount = deccount + 1
dece(deccount) = value(j)
end if

end do
! which month has more data
jmax = jancount
if (jmax .lt. febcount) then
jmax = febcount
else if (jmax .lt. marcount) then
jmax = marcount
else if (jmax .lt. aprcount) then
jmax = aprcount
else if (jmax .lt. maycount) then
jmax = maycount
else if (jmax .lt. juncount) then
jmax = juncount
else if (jmax .lt. julcount) then
jmax = julcount
else if (jmax .lt. augcount) then
jmax = augcount
else if (jmax .lt. sepcount) then
jmax = sepcount
else if (jmax .lt. octcount) then
jmax = octcount
else if (jmax .lt. novcount) then
jmax = novcount
else if (jmax .lt. deccount) then
jmax = deccount
end if


Write(1,11)
! print the data
do i=1, 31
do j = 1, jmax-1

if(day(j) .eq. i) then
write (1, '(I2,(12F10.3))') i, janu(j), febr(j), marc(j), apri(j), mayo(j), june(j), july(j), augu(j), sept(j), octo(j), nove(j), dece(j)
else
write(1,'(I2,(12F10.3))') i
end if
end do
end do
11 Format('Nob', 6x,'Jan', 7x,'Feb', 7x,'Mar', 7x,'Apr', 7x,'May', 7x,'Jun', 7x,'Jul', 7x,'Aug', 7x,'Sep', 7x,'Oct', 7x,'Nov', 7x,'Dec')
end do opendo
close(unit=3)
close(unit=1)

end program y_m5

 
Your close needs to be inside the do loop
Code:
close(unit=3)
close(unit=1)
end do opendo
 
Yes i tried that. It did not work, i am having the same problem. It's still passing the values to the next file. thanks again.
 
The close fix will close the files at the end of every loop. If you do not do that as a matter of practice, you will find that your programs will magically run out of memory or file handles. On Unix, if this calls fopen in libc, then you're restricted to 255 file handles.

After label 1040 put in a print statement with what file1, file2 and status are. It will probably tell you when it goes wrong.
 
thanks i did it and i am getting iostat values of -1 for all the input files (file2) and 0 for the output file(file1).
could this be because i have files with no data as input files?
 
Oops - I meant before openif.

Question: are there 5000 entries for each data file? What happens if there are less? Say you have 4000 in 60 and 3500 in 61. The values between 3501 and 4000 will contain values from the previous year.
 
ok, before the openif both status are 0 for all the files.
No there are not 5000 entries for this each file. It is variable for each file, some can have 300 entries and other 50 or 5.
 
Declare an integer ndlast then try this
Code:
       read(3,*, IOSTAT=status, end=1050) (day(z), month(z), value(z), z=1,nd)
       else openif
       write(*,1040) status
          end if openif
   
   1040 format (' ', 'Error opening the file: IOSTAT=', I6)
   1050 ndlast = z - 1   
   
   jancount = 0
febcount = 0
marcount = 0
aprcount = 0
maycount = 0
juncount = 0
julcount = 0
augcount = 0
sepcount = 0
octcount = 0
novcount = 0
deccount = 0

do j=1, ndlast
 
thanks. It did not work. It is still passing values to the next file with data.
 
ok now is working. Thanks.
I have other doubt now. I have daily values for each month and I'm trying to write them in the output file but i want it to write it in order. I mean for example for jun i have data for the days 3 and 9 and for dec i have data for the days 15 and 30. For some months i'll have just few or no data but for other i'll have everyday with data.

day J F M A M J J A S O N D
1
2
3 x
.
.
9 x
.
.
.
15 x
.
.
.
30 x



Now it is writing me the data just in one line.

Thanks a lot again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top