Reputation: 245
I am writing codes in Fortran 95, to read a number of files looking like 1.dat, 2.dat, ......, 9999.dat. I have a code that reads 0001.dat, 0002.dat, .......... 9999.dat. It looks like
character*12, fn
..
DO i=1,n
write(fn,*)i
open(1,file=fn)
do j=1,5
read(1,*)x(i,j),y(i,j),z(i,j)
end do
10 format(i4.4,'.dat')
May you suggest me how can I make it possible to read the files that I have please? Thanks.u
Upvotes: 0
Views: 2731
Reputation: 9514
What if you change your format to
10 format (i4,'.dat')
I tried this for instance
character *8 filename
do i=1,9999
write (filename, 10) i
write (*,*) filename
end do
10 format (i4,'.dat')
end
And I had no leading zeros in my file names.
Is this hat you were looking for ?
Update
I see...
Then the format should be
10 format (i0,'.dat')
the '0' means left justified.
I tested it with the following pgm
character*12, fn
integer x(11,5)
integer y(11,5)
integer z(11,5)
do i=1,11
write(fn,10)i
open(1,file=fn)
do j=1,5
read(1,*)x(i,j),y(i,j),z(i,j)
end do
close(1)
end do
10 format(i0,'.dat')
end
And that worked for me.
Update 2
implicit none
integer n,ns,i,j
real x(9999,400),y(9999,400),z(9999,400),a(9999,400),aa(400)
character*12, fn
n = 14
ns = 2
do i = 0,n
do j = 1, 5
a(i,j) = 0.0
end do
end do
do i=1,n
write(fn,10)i
open(1,file=fn)
do j=1,5
read(1,*)x(i,j),y(i,j),z(i,j)
if(i.le.ns) then
a(i,j) = x(i,j)
else
a(i,j) = x(i,j) + a(i-ns,j)
end if
aa(j) = a(i,j)
write(*,*) j,x(i,j),y(i,j),z(i,j),a(i,j)
end do
close(1)
do j = 1, 5
write(*,*) aa(j)
end do
end do
10 format(i0,'.dat')
end
Worked for me.
Output (on Ubuntu 10.10).
./a.out
1 1.0000000 3.0000000 5.0000000 1.0000000
2 2.0000000 5.0000000 7.0000000 2.0000000
3 3.0000000 7.0000000 8.0000000 3.0000000
4 1.0000000 4.0000000 8.0000000 1.0000000
5 1.0000000 4.0000000 8.0000000 1.0000000
1.0000000
2.0000000
3.0000000
1.0000000
1.0000000
1 3.0000000 8.0000000 5.0000000 3.0000000
2 7.0000000 5.0000000 7.0000000 7.0000000
3 3.0000000 7.0000000 8.0000000 3.0000000
4 1.0000000 4.0000000 10.000000 1.0000000
5 3.0000000 5.0000000 7.0000000 3.0000000
3.0000000
7.0000000
3.0000000
1.0000000
3.0000000
1 3.0000000 8.0000000 5.0000000 4.0000000
2 7.0000000 5.0000000 7.0000000 9.0000000
3 3.0000000 7.0000000 8.0000000 6.0000000
4 1.0000000 4.0000000 10.000000 2.0000000
5 3.0000000 5.0000000 7.0000000 4.0000000
4.0000000
9.0000000
6.0000000
2.0000000
4.0000000
1 3.0000000 8.0000000 5.0000000 6.0000000
2 7.0000000 5.0000000 7.0000000 14.000000
3 3.0000000 7.0000000 8.0000000 6.0000000
4 1.0000000 4.0000000 10.000000 2.0000000
5 3.0000000 5.0000000 7.0000000 6.0000000
6.0000000
14.000000
6.0000000
2.0000000
6.0000000
1 3.0000000 8.0000000 5.0000000 7.0000000
2 7.0000000 5.0000000 7.0000000 16.000000
3 3.0000000 7.0000000 8.0000000 9.0000000
4 1.0000000 4.0000000 10.000000 3.0000000
5 3.0000000 5.0000000 7.0000000 7.0000000
7.0000000
16.000000
9.0000000
3.0000000
7.0000000
1 3.0000000 8.0000000 5.0000000 9.0000000
2 7.0000000 5.0000000 7.0000000 21.000000
3 3.0000000 7.0000000 8.0000000 9.0000000
4 1.0000000 4.0000000 10.000000 3.0000000
5 3.0000000 5.0000000 7.0000000 9.0000000
9.0000000
21.000000
9.0000000
3.0000000
9.0000000
1 3.0000000 8.0000000 5.0000000 10.000000
2 7.0000000 5.0000000 7.0000000 23.000000
3 3.0000000 7.0000000 8.0000000 12.000000
4 1.0000000 4.0000000 10.000000 4.0000000
5 3.0000000 5.0000000 7.0000000 10.000000
10.000000
23.000000
12.000000
4.0000000
10.000000
1 3.0000000 8.0000000 5.0000000 12.000000
2 7.0000000 5.0000000 7.0000000 28.000000
3 3.0000000 7.0000000 8.0000000 12.000000
4 1.0000000 4.0000000 10.000000 4.0000000
5 3.0000000 5.0000000 7.0000000 12.000000
12.000000
28.000000
12.000000
4.0000000
12.000000
1 3.0000000 8.0000000 5.0000000 13.000000
2 7.0000000 5.0000000 7.0000000 30.000000
3 3.0000000 7.0000000 8.0000000 15.000000
4 1.0000000 4.0000000 10.000000 5.0000000
5 3.0000000 5.0000000 7.0000000 13.000000
13.000000
30.000000
15.000000
5.0000000
13.000000
1 3.0000000 8.0000000 5.0000000 15.000000
2 7.0000000 5.0000000 7.0000000 35.000000
3 3.0000000 7.0000000 8.0000000 15.000000
4 1.0000000 4.0000000 10.000000 5.0000000
5 3.0000000 5.0000000 7.0000000 15.000000
15.000000
35.000000
15.000000
5.0000000
15.000000
1 3.0000000 8.0000000 5.0000000 16.000000
2 7.0000000 5.0000000 7.0000000 37.000000
3 3.0000000 7.0000000 8.0000000 18.000000
4 1.0000000 4.0000000 10.000000 6.0000000
5 3.0000000 5.0000000 7.0000000 16.000000
16.000000
37.000000
18.000000
6.0000000
16.000000
At line 23 of file c.for (unit = 1, file = '12.dat')
Fortran runtime error: End of file
Upvotes: 2