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!

Reading files inside many numbered folders

Status
Not open for further replies.

Vahid9

Technical User
Jul 28, 2016
23
CA
Hello,

I have a number of folders labeled K00001 to K00100 in my scratch folder. Each folder contains a file called a.dat.

From my scratch folder, I would like to go into each folder, read the content of a.dat, save the necessary information to an array and then move on to the next folder.

I declared a CHARACTER as such:

CHARACTER(*), PARAMETER :: fileplace ="/home/vahid/scratch/K"

The I run a DO loop over the folders:

DO f=1,100
OPEN(unit=100,'(a,i5,a)') 'fileplace',f,'/a.dat'
read the file, etc.
ENDDO

The OPEN statement results in the following error:

error #5082: Syntax error, found IDENTIFIER 'FILEPLACE' when expecting one of: <END-OF-STATEMENT> ;
OPEN(unit=100,'(a,i5,a)') fileplace,f,/a.dat

Any suggestions as to how I can resolve this error would be a great help.

Thank you,

Vahid
 
You need to create the filename in a buffer first
Code:
character(256):: filename
...
do f = 1, 100
   ! create the filename
   write(filename,'(a,i5,a)') fileplace, f, 'a.dat'
   ! open the file
   open(unit = 100, file=filename)
   ...
end do
 
Thanks xwb. It worked. I just had to use i5.5 instead of i5 in the format statement to get the leading zeros.

Best wishes,

Vahid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top