Fortran Fortran Write Path - Change File Paths

  • Thread starter Thread starter shade rahmawati
  • Start date Start date
  • Tags Tags
    Fortran Path
AI Thread Summary
The discussion revolves around a Fortran code that generates multiple files with specific naming conventions. The original code uses a series of IF statements to format file names based on a loop index. A user seeks guidance on how to change the file paths to a different folder within the main program directory. The solution involves adding a character variable for the path and concatenating it with the file name when opening the files. Additionally, suggestions are made to improve code readability by using a SELECT CASE statement instead of multiple IF statements and incorporating the format directly into the WRITE statements. The user expresses gratitude for the assistance, indicating the solution resolved their issue.
shade rahmawati
Messages
6
Reaction score
1
Hello,

I have write a fortran code to write several file, like this

CHARACTER(len=12) :: FNAME
INTEGER::ST

DO 100 ST=1,12
DO KC=1,4 !number of layer
IF(ST.EQ.1) WRITE (FNAME,10)KC
IF(ST.EQ.2) WRITE (FNAME,11)KC
IF(ST.EQ.3) WRITE (FNAME,12)KC
IF(ST.EQ.4) WRITE (FNAME,13)KC
IF(ST.EQ.5) WRITE (FNAME,14)KC
IF(ST.EQ.6) WRITE (FNAME,15)KC
IF(ST.EQ.7) WRITE (FNAME,16)KC
IF(ST.EQ.8) WRITE (FNAME,17)KC
IF(ST.EQ.9) WRITE (FNAME,18)KC
IF(ST.EQ.10) WRITE (FNAME,19)KC
IF(ST.EQ.11) WRITE (FNAME,20)KC
IF(ST.EQ.12) WRITE (FNAME,21)KC
WRITE(6,*)FNAME
OPEN(1,FILE=FNAME)

CLOSE(1)
END DO
10 FORMAT('XY01UA',I2.2,'.DAT')
11 FORMAT('XY02UA',I2.2,'.DAT')
12 FORMAT('XY03UA',I2.2,'.DAT')
13 FORMAT('XY04UA',I2.2,'.DAT')
14 FORMAT('XY05UA',I2.2,'.DAT')
15 FORMAT('XY06UA',I2.2,'.DAT')
16 FORMAT('XY07UA',I2.2,'.DAT')
17 FORMAT('XY08UA',I2.2,'.DAT')
18 FORMAT('XY09UA',I2.2,'.DAT')
19 FORMAT('XY10UA',I2.2,'.DAT')
20 FORMAT('XY11UA',I2.2,'.DAT')
21 FORMAT('XY12UA',I2.2,'.DAT')
100 CONTINUE
STOP
ENDThose codes are works fine.
I only want to know, how to change all of this files' path to another folder inside the main program folder.

Thanks in advance,
Shade
 
Technology news on Phys.org
Add another character variable containing the path, then concatenate both when opening:
Fortran:
CHARACTER(len=80) :: PATH
PATH = "./subfolder/"

open (1,FILE=trim(PATH)//FNAME)
The use of trim is necessary if PATH has a length greater than the actual path name.

Note that, for better practice, the series of if statements should be replace by a select case. Your program could also be more readable by incorporating the format inside the write statements, e.g., use
Fortran:
WRITE (FNAME,"('XY01UA',I2.2,'.DAT')") KC
instead of
Fortran:
WRITE (FNAME,10)KC
! lots of code
10 FORMAT('XY01UA',I2.2,'.DAT')
 
  • Like
Likes shade rahmawati
DrClaude, you save the day! Thank you very much. Now it's all good.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
6
Views
2K
Replies
12
Views
3K
Replies
3
Views
3K
Replies
2
Views
25K
Replies
2
Views
2K
Replies
3
Views
7K
Replies
3
Views
4K
Replies
6
Views
5K
Replies
3
Views
2K
Back
Top