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!

Open several files 1

Status
Not open for further replies.

bhussin74

Technical User
Feb 6, 2004
7
GB
Hi guys.... need your kindly help

I have a problem where I need to open few files in my main program. As example let say I have file1.dat file2.dat file3.dat and so on.
My question is in my main program I have to open the all files in a loop

program
do 10 i = 1,5 {file to open}
open file(i).dat
....
10 continue

Please help me
 
Maybe you should use units 11 - 15 as 1 and 5 may be in use
as defaults.
C Sample program
character * 2 f(5)
data f/'11','12','13','14','15'/
do 10 i=11,15
10 open(i,file='FILE'//f(i)//'.dat',status='new')
end
 
Correction :
10 open(i,file='FILE'//f(i-10)//'.dat',status='new')
 
Thanks PAnderson..

It works ...

But my problem is still unsettle

Let say I have 5 files where in each files contains 2 colums of reading x and y.
My program will read all the data file in a looping. (You giving me help on this ..)
Anyway I try but it doesnt work. I copy you the program

program openfile
IMPLICIT NONE
INTEGER :: i
character(2):: f(5)
double precision :: x,y

data f/'11','12','13','14','15'/

do 10 i = 11,15

open(UNIT =i,file='test'//f(i-10)//'.txt')

100 CONTINUE
READ (UNIT=11, FMT=*, END=200) x,y

print *,x,y
....
.....
GO TO 100

200 CONTINUE

close(i)

end

test1.txt and other data file contains some thing like below
2 4.53602
4 4.168
6 5.632328
8 5.051513
10 4.077689
12 1.705956

I don't why it can't work. I spent nearly 2 hours for it but ends with frustrating. Please help me
 
You are missing statement 10 (end of DO-loop), and you
are reading from unit 11 (only).
Try :
do 10 i = 11,15
open(UNIT =i,file='test'//f(i-10)//'.txt')
5 READ (UNIT=i, FMT=*, END=10) x,y

print *,x,y
....
.....
GO TO 5
10 close(i)

end
 
Hi ... still trying to open several files in a loop. The problem is when i use the example above its work but it doesn't when i try use my data

its work when i'm call my file like this

open(11,FILE ='test1.txt'),
read(11,FMT=*,END = 200)x,y

200 continue

but when i'm trying to change the first line to

open(11,FILE = 'test'//f(i-10)//'.txt)
it doesn't read the x and y
why?


 
Maybe, if we try to simplify the filename :
character * 9 fname(5)
data fname /'test1.txt','test2.txt','test3.txt','test4.txt','test5.txt'/
do 10 i = 11,15
open(UNIT =i,file=fname(i-10))
5 READ (UNIT=i, FMT=*, END=10) x,y
print *,x,y
.....
GO TO 5
10 close(i)
end
 
If you still have problems with opening or reading from the files, it may be an idea to add an ERR exit in the
open and the read statement to see what is going wrong.
 
thank you so much to PAnderson for you help. I'm really appreciate it.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top