0

As there are lots of folders and files, I wish to use loop to open the folder and read them.

The names of the folders to open and read them are rt20, rt30, rt40 and so on. Whereas, the names of the folders to save some text files are sphere20, sphere30, etc. I created an array of characters and concatenate with the name of the folders but failed.

The actual code that I created is:

  PROGRAM  TEST1
  IMPLICIT NONE
  INTEGER ::lskip,lread,count
  INTEGER :: n,I,j
  REAL l,b,c,d,e,ReThe,ImThe,RePhi,ImPhi
  CHARACTER(len=100) :: FN,FNL
  character*2,parameter :: A(*) = ['20','30','40','50','60','70']


  N=256
  do j = 1,6
  OPEN(11,file='rt',A(j),'\\output.dat',form='formatted')
  !--allocate your vectors here
  DO I=1,N
  WRITE(FN,10)I
  WRITE(6,*)FN
  OPEN(1,FILE=FN)
  !skip first 17 lines
  do lskip = 1,18
  READ(1,*)
  end do
  ! Now read actual lines
  do lread = 1,1
  READ(1,*)l,b,ReThe,ImThe,RePhi,ImPhi,c,d,e

  end do
  write(11,20)ReThe,ImThe,RePhi,ImPhi



  CLOSE(1)
  END DO
10    format ('pm\\vertical\\sphere',A(j),'\\n_FarField',I0,'.ffe')

20    format (E14.7,4X,E14.7,4X,E14.7,4X,E14.7)


  end do
  END PROGRAM

The errors that are appeared in the program:

In file C:\Users\Hamsalekha\Desktop\far_field_sphere\Source1.f:12

      OPEN(11,file='rt',A(j),'\\output.dat',form='formatted')
                        1
Error: Syntax error in OPEN statement at (1)
In file C:\Users\Hamsalekha\Desktop\far_field_sphere\Source1.f:33

10    FORMAT('pm\\vertical\\sphere',A(j),'\\n_FarField',I0,'.ffe')
                                      1
Error: Unexpected element in format string at (1)

I couldn't find a solution on any websites. I really appreciate if someone can help me to solve this problem or give any hint.

  • @HighPerformanceMark Yes, I even closed it as a duplicate of it at first but then realized that `A` is actually a character array so no conversion is involved in the first error and the error in the FORMAT statement is not directly related either link. – Vladimir F Героям слава Jun 20 '20 at 09:16
  • I've gone for "duplicate" because it's helpful to address the underlying problem, not the symptoms of bad string concatenation (which is covered in answers there) or incorrect format strings. – francescalus Jun 20 '20 at 09:18

1 Answers1

0

You are concatenating your strings incorrectly:

OPEN(11,file='rt',A(j),'\\output.dat',form='formatted')

This only asks for file "rt" and than adds some other strange arguments. You need

OPEN(11,file='rt'//A(j)//'/output.dat',form='formatted')

Be aware that \ does not work as an escaping character in Fortran strings and that / is the proper multiplatform character to separate directories in the hierarchy. Yes, even in MS Windows.

The // is the Fortran string concatenation operator.

Jean-Claude Arbaut mentions in the comment the reason for the other error. One needs a constant string in the FORMAT statement. I personally strongly prefer to use format strings in the write statements directly and also to put what I can in the output list:

 WRITE(FN,'(3A,I0,A)') 'pm/vertical/sphere',A(j),'/n_FarField',I,'.ffe'

but there are many other ways to write it. You can concatenate the string. You can also use the FORMAT statement.

      WRITE(FN,10) A(j), I
10    FORMAT('pm/vertical/sphere',A,'/n_FarField',I0,'.ffe')

Many examples can be found here: Convert integers to strings to create output filenames at run time

  • Thank you for your reply, Vladimir. I tried for the first part of the code and it was correct. However, for the second part of the code, I still receive error although I made adjustment as you said. The role of the second part of the code is to open the multiple directories and read the data. I will place the second line of the code in the next comment. – Hamsa Lekha Jun 20 '20 at 08:31
  • 10 FORMAT('pm/vertical/sphere'//A(j)//'/n_FarField',I0,'.ffe') 1 Error: Unexpected element in format string at (1) – Hamsa Lekha Jun 20 '20 at 08:32
  • I am not sure how to make an adjustment for this error. – Hamsa Lekha Jun 20 '20 at 08:32
  • @HamsaLekha You can't use the concatenation operator, or any operator, in the format string. Format a string (A format) and build the string in the write statement or elsewhere. –  Jun 20 '20 at 08:33
  • It still shows error even if I don't use the concatenation operator. For example: FORMAT('pm/vertical/sphere',A(j),'/n_FarField',I0,'.ffe') – Hamsa Lekha Jun 20 '20 at 08:37
  • @HamsaLekha You can only put constant strings (in quotes) and format characters in a format statement. Your `A(j)` can't be part of a format string either. Replace it with an `A` format and put `A(j)` in the write statement. –  Jun 20 '20 at 08:41
  • Yes, I will add it, at least one way to do it, I had to stop and forgot to add the rest. – Vladimir F Героям слава Jun 20 '20 at 08:42
  • I tried this way but still shows error `WRITE(10,*)A(j)` `10 FORMAT('pm/vertical/sphere',A,'/n_FarField',I0,'.ffe')` The error is: WRITE(10,*)A(j) 1 Error: Function 'a' at (1) has no implicit type – Hamsa Lekha Jun 20 '20 at 09:01
  • @HamsaLekha It works for me, check the the array `A(j)` is properly declared where you have the `WRITE` statement. – Vladimir F Героям слава Jun 20 '20 at 09:19
  • Thank you very much, Vladimir F and Jean-Claude Arbaut. It worked perfectly for me. I really appreciate your help as it was one of the crucial part of my research. Thanks again! – Hamsa Lekha Jun 20 '20 at 10:44