1

Is it possible in Fortran to open several folders, read the file inside it (there is only one file in each folder and each has the same filename), and then write the data (array 2x40 data, but only the second column will be taken) into a single file?

I tried to write the program that Folders name are in real number (0, 0.05, ... ,10), change it into character and opened in every loop as written below, but seems the line to open folders and file don't work.

program results
implicit none
integer, parameter :: nx = 100, ny = 40, lmax = 2, mmax=40
character(*), parameter :: fileplace = "/home/sampleDict"
character(*) :: string
integer :: i=0, k=0
integer :: l,m
real(8) :: fn=0
integer, dimension(lmax, mmax) :: A
open (unit=200,file='elevation.csv', status='unknown')
do i=i+1,nx
 fn=fn+0.05
 write(string,*) fn
 open(unit=i,file=fileplace//trim(adjustl(string))//'data.csv',status='old')
 read(i,*) ((A(l,m),l=1,lmax),m=1,mmax)
 write(200,*) A(2,m)
enddo
end program
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • 1
    Of course it is possible. **Never** use it *"does not work"*. It does not help at all. Tell us what happens. A crash? Wrong result? Why is it wrong? Any messages? – Vladimir F Героям слава May 26 '17 at 09:19
  • Probably you need a specific format instead of the `*` in `write(string,*)` but we *really* need more details. Tell us the *exact* names of the directories (folders). See also this https://stackoverflow.com/questions/1262695/convert-integers-to-strings-to-create-output-filenames-at-run-time , but that is about integers. In **Linked** section on the right it may have an example with reals. – Vladimir F Героям слава May 26 '17 at 09:22
  • I'd suggest you write your file name string to the console. You will find you file names are not what you expect due to floating point representation issues. You also don't appear to have any directory separators in the name. – agentp May 26 '17 at 12:16
  • Definitely. 0.05 cannot be represented exactly in binary, so you get numbers like 5.0000000745058060E-002 and 0.10000000149011612. – Jack May 26 '17 at 13:18
  • A quick and dirty workaround (if the number of the files is, say, only 21) may be to make a character array that contains strings like "0", "0.05", "0.1", ..., "1" and use them in a do-loop (say, open(..., file = fileplace // trim(tags(i)) // "data.csv", ...)). Another workaround is to read such tags from an external file that contains a sequence of values. (The latter file could be prepared manually, or by any other tools.) – roygvib May 28 '17 at 03:12
  • Thanks for the answers. The main problem was what Jack said. It gave error due to failure of converting real number into string. – Hicha Aquino Jun 26 '17 at 08:10

0 Answers0