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!

How to use GET_FILES@ command? 2

Status
Not open for further replies.

eriehml

Technical User
Jul 17, 2009
9
US
I have several files with extention .dat in a folder where I run my fortran code. I would like to use GET_FILES fortran command to read automatically these files names from the working folder. Could you provide me a simple example on how to use GET_FILES@ subroutine in Fortran? Your help is very much appreciated. Thanks.
 
I doubt if there is an intrinsic function GET_FILES in Fortran. Maybe some of the commercial compilers have such function but you didn't specified the compiler.

Generally, you have in fortran the system() function to execute every command and you can redirect command output into a file - and that's all what you need.

Here is an example how to read specified file names from the working folder into an fortran array:
get_files.f90
Code:
[COLOR=#a020f0]program[/color] get_files
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]256[/color]), [COLOR=#2e8b57][b]allocatable[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:) :: files_array
  [COLOR=#2e8b57][b]integer[/b][/color] :: cmd_rc [COLOR=#0000ff]! command return code[/color]
  [COLOR=#0000ff]! command string - outptut command into a file[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]parameter[/b][/color] :: cmd_string [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"ls *.dat > files.txt"[/color]
 
  cmd_rc [COLOR=#804040][b]=[/b][/color] system (cmd_string)
  [COLOR=#0000ff]! if command not succesfull then stop the program[/color]
  [COLOR=#804040][b]if[/b][/color] ( cmd_rc [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color] ) [COLOR=#804040][b]stop[/b][/color] [COLOR=#ff00ff]'system: Error !!!'[/color]

  [COLOR=#0000ff]! read data from the output file into en array[/color]
  [COLOR=#a020f0]call[/color] file2array([COLOR=#ff00ff]'files.txt'[/color], files_array)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#0000ff]! next line[/color]

  [COLOR=#0000ff]! print array to the screen[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Array of files: '[/color]
  [COLOR=#a020f0]call[/color] array2screen([COLOR=#ff00ff]'file'[/color], files_array)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
  
[COLOR=#a020f0]contains[/color]
[COLOR=#0000ff]! ********************** Functions/Procedures *************************[/color]
[COLOR=#a020f0]subroutine[/color] file2array(fname, array)
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: fname
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]allocatable[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color] [COLOR=#2e8b57][b]out[/b][/color]) :: array
  [COLOR=#2e8b57][b]integer[/b][/color] :: i, nr_lines, nr_elements, stat
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]30[/color]) :: dummy

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"Reading the Data from file '"[/color], fname, [COLOR=#ff00ff]"' :"[/color] 
  [COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color]fname,[COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color],[COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat)
  [COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) fname, [COLOR=#ff00ff]' cannot be opened !'[/color]
    [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]20[/color]   
  [COLOR=#804040][b]end if[/b][/color]

  [COLOR=#0000ff]! count number of lines in the file[/color]
  nr_lines [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
  [COLOR=#804040][b]do[/b][/color]
    [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]end[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]10[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]20[/b][/color]) dummy
    nr_lines [COLOR=#804040][b]=[/b][/color] nr_lines [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
  [COLOR=#804040][b]end do[/b][/color]

  [COLOR=#0000ff]! rewind back to the beginning of the file for unit=1[/color]
  [COLOR=#6a5acd]10[/color] [COLOR=#804040][b]rewind[/b][/color]([COLOR=#ff00ff]1[/color])
  [COLOR=#0000ff]! number of array elements = number of data lines in the file[/color]
  nr_elements[COLOR=#804040][b]=[/b][/color]nr_lines
  [COLOR=#0000ff]! allocate the arrays of given size [/color]
  [COLOR=#804040][b]allocate[/b][/color] (array(nr_elements))

  [COLOR=#0000ff]! read array elements from the file[/color]
  [COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], nr_elements
    [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]20[/b][/color]) array(i)
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(a)'[/color],[COLOR=#804040][b]advance[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'no'[/color]) [COLOR=#ff00ff]'.'[/color]
  [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#0000ff]! next line[/color]
  [COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]1[/color])

  [COLOR=#0000ff]![/color]
  [COLOR=#6a5acd]20[/color] [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'End of Reading Data'[/color]
[COLOR=#a020f0]end subroutine[/color] file2array

[COLOR=#a020f0]subroutine[/color] array2screen(array_name, array)
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: n, i
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]allocatable[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: array
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]) :: array_name

  [COLOR=#0000ff]! process only if the array is allocated[/color]
  [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]allocated[/color](array)) [COLOR=#804040][b]then[/b][/color]
    n [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]size[/color](array)
    [COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], n
      [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) array_name, [COLOR=#ff00ff]'['[/color], i, [COLOR=#ff00ff]'] = '[/color], [COLOR=#008080]trim[/color](array(i))
    [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]else[/b][/color]
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Error: Argument Array not allocated !'[/color]
  [COLOR=#804040][b]end if[/b][/color]
[COLOR=#a020f0]end subroutine[/color] array2screen

[COLOR=#a020f0]end program[/color] get_files
Output:
Code:
$ g95 get_files.f90 -o get_files

$ get_files
 Reading the Data from file 'files.txt' :
......
 End of Reading Data

 Array of files: 
 file[ 1 ] = cubes.dat
 file[ 2 ] = inpt.dat
 file[ 3 ] = inpt_.dat
 file[ 4 ] = inpt_dos.dat
 file[ 5 ] = input.dat
 file[ 6 ] = measurement.dat
 
Mikrom. Thanks for your reply. It is very instructive the way you program and I am definitely taking advice from the way you code to help myself. I know how to read unknown number of lines from an existing file and put it into an array, that is not my issue. My issue is to read from a working folder all the files with extension .dat, I mean, in the folder (not in a file) there would be several files (a.dat, b.dat, c.dat) and I need to put these names into an array. Because of your obvious expertise I am sure you know how, so whenever you have some time please let me know. I work in a laptop PC, windows Vista, compiler is Plato (it compiles all fortrans F77, F90 and F95 but not gfortran). Thanks a lot.

--
 
Here is the documentation for Silverfrost/Salford Fortran. Plato is the IDE: not the version of Fortran.
Looks like what you need is
Code:
character*64 files(100)
integer(kind=2) nfiles, ierr
call get_files@('*.dat', files, 100, nfiles, ierr);
 
eriehml said:
My issue is to read from a working folder all the files with extension .dat, I mean, in the folder (not in a file) there would be several files (a.dat, b.dat, c.dat) and I need to put these names into an array.

Hi eriehml,
Exactly that I do in the example I posted above, but in 2 steps:
1. With the system command
Code:
ls *.dat > files.txt
I list all *.dat files and redirect the output into a file named files.txt. So the command creates the file named files.txt which lines contain the names of *.dat files.
In my case files.txt contain these lines:
Code:
cubes.dat
inpt.dat
inpt_.dat
inpt_dos.dat
input.dat
measurement.dat
2. Then I process the file files.txt, i.e. I read the lines into an array.

Hovever if you use only silverfrost fortran, which has a specialized routine for this, then it's easier for you to use it as xwb suggested.
 
Mikrom, thanks again, now I understand what you mean, pretty good idea. I use DOS, so I can do
copy *.log files.txt
but when I compile system() is not an option. In fact I can not find the system() command you mention, it is actually not listen in
Other than using system() do you know any other way?
Thanks.
 
I mean
copy *.dat files.txt
sorry for the typo.
 
system is a subroutine: not a function. Change the line to
Code:
cmd_rc = 0
call system (cmd_string)
Just assume that the call was successful. It is a standard subroutine call so it won't be in the F77 documentation.
 
Hi eriehml,
If you use Windows then use the DOS command DIR with the bare format option /B (see the code)

As xwb said system() is a subroutine, but in GNU fortran it is implemented also as an function
and so I rather use it as an function, because I can evaluate the return code to see if the system call was succesful or not.

Here are the modifications in main program which should now work for you (the functions/procedures part doesn't change)
Code:
[COLOR=#a020f0]program[/color] get_files
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]256[/color]), [COLOR=#2e8b57][b]allocatable[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:) :: files_array
  [COLOR=#2e8b57][b]integer[/b][/color] :: cmd_rc [COLOR=#0000ff]! command return code[/color]
  [COLOR=#0000ff]! command string - outptut command into a file[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]parameter[/b][/color] :: cmd_string [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"dir *.dat /B > files.txt"[/color]
 
  [COLOR=#a020f0]call[/color] system (cmd_string)

  [COLOR=#0000ff]! read data from the output file into en array[/color]
  [COLOR=#a020f0]call[/color] file2array([COLOR=#ff00ff]'files.txt'[/color], files_array)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#0000ff]! next line[/color]

  [COLOR=#0000ff]! print array to the screen[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Array of files: '[/color]
  [COLOR=#a020f0]call[/color] array2screen([COLOR=#ff00ff]'file'[/color], files_array)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
  
[COLOR=#a020f0]contains[/color]
[COLOR=#0000ff]! ********************** Functions/Procedures *************************[/color]

In GNU fortran the intrinsic subroutine system() have an second optional parameter (but I don't know if it is standard for other compilers too), so in GNU fortran I would use instead of above simple call this with error detecting
Code:
  [COLOR=#a020f0]call[/color] system (cmd_string, cmd_rc)
  [COLOR=#0000ff]! if command not succesfull then stop the program[/color]
  [COLOR=#804040][b]if[/b][/color] ( cmd_rc [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color] ) [COLOR=#804040][b]stop[/b][/color] [COLOR=#ff00ff]'system: Error !!!'[/color]
 
Mikrom. Absolute fantastic. The "call system()" worked perfectly and it is beyond my understanding why it is not included in the 77library.pdf. The use of call system() which I didn't know opens up a wide variety of stuff I can do from my fortran code, right now I envision plotting my data to see how say a curve fitting is going and then automatically return to my fortran code. This is great. You are also correct the DIR *.dat /B > files.txt creates correctly a file with all the names in working folder. As for the rest of your code I haven't tried it nevertheless my issue is resolved. With your advice there is a lot more potential than just doing what I originally wanted. Originally I wanted to use GET_FILES@ but for some reason didn't work for me although it is in the 77library.pdf. Anyway, I doubt that I will be of any coding-help for you so I guess I want to express my sincere gratitude to you, thanks for your advice, and coding lesson.
 
Hi eriehml,

Which call system() worked in your fortran?
This with one argument
Code:
call system (cmd_string)
or this with two arguments ?
Code:
call system (cmd_string, cmd_rc)
 
Mikrom, I used call system(cmd_string).
I run your code with subroutines and all, one error "(941) ARRAY is dummy argument and so cannot be ALLOCATABLE". This happens in both subroutines were character ARRAY is defined. Would you know how to fix it?
 
I only know the GNU compilers g77, gfortran and g95. The example I posted compiles fine with gfortran and g95.

Have you exactly copied the example I posted and have you used the magic word contains which separates the main program from the procedures?
When you want to use allocatable arrays as arguments you need to use contains as I done, or you need to place the subroutines into a module and use the module in your program (however if contains or modules are supported by your compiler).
Try it and if you have problems, I can post the version using a module here.
 
Mikrom, hi. It is super instructive reading your posts. Yes I definitely used CONTAINS. It gives me the error mentioned in previous post. My compiler supports modules so that should work. I am trying to fix it myself but no success yet, I thought I could use INTERFACE so that subroutines know about "nr_elements" in advance and avoid using Allocatable that is, define ARRAY size from the definition. It is not working for me yet.
 
Hi eriehml,
I created this version with module. Try if it works for you or not. If not, then the silvefrost fotran doesn't support allocatable arrays as subroutine arguments.
Code:
[COLOR=#a020f0]module[/color] myIO
[COLOR=#a020f0]contains[/color]
[COLOR=#0000ff]! ********************** Functions/Procedures *************************[/color]
  [COLOR=#a020f0]subroutine[/color] file2array(fname, array)
    [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
    [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: fname
    [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]allocatable[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color] [COLOR=#2e8b57][b]out[/b][/color]) :: array
    [COLOR=#2e8b57][b]integer[/b][/color] :: i, nr_lines, nr_elements, stat
    [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]30[/color]) :: dummy

    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"Reading the Data from file '"[/color], fname, [COLOR=#ff00ff]"' :"[/color] 
    [COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color]fname,[COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color],[COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat)
    [COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) fname, [COLOR=#ff00ff]' cannot be opened !'[/color]
      [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]20[/color]   
    [COLOR=#804040][b]end if[/b][/color]

    [COLOR=#0000ff]! count number of lines in the file[/color]
    nr_lines [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
    [COLOR=#804040][b]do[/b][/color]
      [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]end[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]10[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]20[/b][/color]) dummy
      nr_lines [COLOR=#804040][b]=[/b][/color] nr_lines [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
    [COLOR=#804040][b]end do[/b][/color]

    [COLOR=#0000ff]! rewind back to the beginning of the file for unit=1[/color]
    [COLOR=#ff00ff]10[/color] [COLOR=#804040][b]rewind[/b][/color]([COLOR=#ff00ff]1[/color])
    [COLOR=#0000ff]! number of array elements = number of data lines in the file[/color]
    nr_elements[COLOR=#804040][b]=[/b][/color]nr_lines
    [COLOR=#0000ff]! allocate the arrays of given size [/color]
    [COLOR=#804040][b]allocate[/b][/color] (array(nr_elements))

    [COLOR=#0000ff]! read array elements from the file[/color]
    [COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], nr_elements
      [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]20[/b][/color]) array(i)
      [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(a)'[/color],[COLOR=#804040][b]advance[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'no'[/color]) [COLOR=#ff00ff]'.'[/color]
    [COLOR=#804040][b]end do[/b][/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#0000ff]! next line[/color]
    [COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]1[/color])

    [COLOR=#0000ff]![/color]
    [COLOR=#ff00ff]20[/color] [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'End of Reading Data'[/color]
  [COLOR=#a020f0]end subroutine[/color] file2array

  [COLOR=#a020f0]subroutine[/color] array2screen(array_name, array)
    [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
    [COLOR=#2e8b57][b]integer[/b][/color] :: n, i
    [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]allocatable[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: array
    [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]) :: array_name

    [COLOR=#0000ff]! process only if the array is allocated[/color]
    [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]allocated[/color](array)) [COLOR=#804040][b]then[/b][/color]
      n [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]size[/color](array)
      [COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], n
        [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) array_name, [COLOR=#ff00ff]'['[/color], i, [COLOR=#ff00ff]'] = '[/color], [COLOR=#008080]trim[/color](array(i))
      [COLOR=#804040][b]end do[/b][/color]
    [COLOR=#804040][b]else[/b][/color]
      [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Error: Argument Array not allocated !'[/color]
    [COLOR=#804040][b]end if[/b][/color]
  [COLOR=#a020f0]end subroutine[/color] array2screen
[COLOR=#a020f0]end module[/color] myIO

[COLOR=#a020f0]program[/color] get_files
  [COLOR=#a020f0]use[/color] myIO
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]256[/color]), [COLOR=#2e8b57][b]allocatable[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:) :: files_array
  [COLOR=#2e8b57][b]integer[/b][/color] :: cmd_rc [COLOR=#0000ff]! command return code[/color]
  [COLOR=#0000ff]! command string - outptut command into a file[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]parameter[/b][/color] :: cmd_string [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"dir *.dat /B > files.txt"[/color]
 
  [COLOR=#a020f0]call[/color] system (cmd_string, cmd_rc)
  [COLOR=#0000ff]! if command not succesfull then stop the program[/color]
  [COLOR=#804040][b]if[/b][/color] ( cmd_rc [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color] ) [COLOR=#804040][b]stop[/b][/color] [COLOR=#ff00ff]'system: Error !!!'[/color]

  [COLOR=#0000ff]! read data from the output file into en array[/color]
  [COLOR=#a020f0]call[/color] file2array([COLOR=#ff00ff]'files.txt'[/color], files_array)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#0000ff]! next line[/color]

  [COLOR=#0000ff]! print array to the screen[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Array of files: '[/color]
  [COLOR=#a020f0]call[/color] array2screen([COLOR=#ff00ff]'file'[/color], files_array)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
[COLOR=#a020f0]end program[/color] get_files


 
Mikrom. You are right Silverfrost is "no bueno" for allocatable in subroutine. I will try to download the Visual Microsoft Fortran Student edition and if it works I guess I will end up buying it. Gonna have to dish out some $130 bucks, yikes! The other Fortran IDE, with libraries and all I've heard of is the Absoft but they charge $299 for student, so that is a no. Thanks for your help. In the meantime what I did is butcher your code (and I apologize for that) and removed dynamic arrays, so I put 100 lines maximum array (dimension (100)), period. I don't know how to close my post, or maybe the host does that. Anyways, has been a pleasure, I will stick around checking the forum, that is for sure.
 
Hi eriehml,

And what about using a free compiler?
If you are a student and create programs for your study and not for business, why to pay for a compiler?
Truly, I wonder, why so many students and professors prefer commercial fortran compilers and not the free ones.
You have seen, although silverfrost is commercial, it doesn't support features as free compilers: g95 and gfortran.

IMHO it's better to pay a fraction of the money you mentioned above for one or two good books about fortran to improve your knowledge,
than pay that money for a commercial compiler, which will be in 2 years obsolete and/or doesn't support the whole fortran standard.

In the following thread I posted some links where you can download free fortran compilers which I'm using:
 
Hi eriehml,

If silverfrost doesn't support allocatable arrays in the subroutines, you can do it at classic way, i.e define fixed size arrays (e.g. 1000 elements) in the program and in the subroutines. But then when you want to change the size, you need to do it at more then one places, that is in the main program and in the all subroutines used.

To define the array size only at one place in the program, you need a global variable. To define a global variable you can use Fortran90 module, or the Fortran77 common block.
In the following example I'm using common block.
In the main program I define the array of size Nmax, which needs to be parameter (I hope silvefrost support this too).
Then I define the global variable N as common N and I assign to it N = Nmax.
Then in the subroutine file2array I use the global variable common N to define the size of input/output array argument. The subroutine outputs the portion of files_array and the number nr_files. The second subroutine array2screen uses the smaller array argument, which holds only the portion of the files_array of size nr_files

Here is the source:
get_files4.f90
Code:
[COLOR=#a020f0]program[/color] get_files
[COLOR=#0000ff]! *** Maximal number of array elements, change it if you need[/color]
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]parameter[/b][/color] :: Nmax [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1000[/color]
  [COLOR=#0000ff]! ***[/color]

  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]256[/color]), [COLOR=#2e8b57][b]dimension[/b][/color](Nmax) :: files_array
  [COLOR=#2e8b57][b]integer[/b][/color] :: cmd_rc [COLOR=#0000ff]! command return code[/color]
  [COLOR=#0000ff]! command string - outptut command into a file[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]parameter[/b][/color] :: cmd_string [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"dir *.dat /B > files.txt"[/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: nr_files [COLOR=#0000ff]! number of files found[/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: N
  [COLOR=#2e8b57][b]common[/b][/color] N  [COLOR=#0000ff]! N is global variable, holding the value of Nmax[/color]

  N [COLOR=#804040][b]=[/b][/color] Nmax [COLOR=#0000ff]! Store the vaule Nmax into the global variable[/color]
  
  [COLOR=#a020f0]call[/color] system (cmd_string, cmd_rc)
  [COLOR=#0000ff]! if command not succesfull then stop the program[/color]
  [COLOR=#804040][b]if[/b][/color] ( cmd_rc [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color] ) [COLOR=#804040][b]stop[/b][/color] [COLOR=#ff00ff]'system: Error !!!'[/color]

  [COLOR=#0000ff]! read data from the output file into en array[/color]
  [COLOR=#a020f0]call[/color] file2array([COLOR=#ff00ff]'files.txt'[/color], files_array, nr_files)

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#0000ff]! next line[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Number of files found: '[/color], nr_files
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#0000ff]! next line[/color]

  [COLOR=#0000ff]! print array to the screen[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Array of files: '[/color]
  [COLOR=#a020f0]call[/color] array2screen([COLOR=#ff00ff]'file'[/color], files_array, nr_files)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
[COLOR=#a020f0]end program[/color] get_files

[COLOR=#0000ff]! ********************** Functions/Procedures *************************[/color]
[COLOR=#a020f0]subroutine[/color] file2array(fname, array, nr_elements)
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: fname
  [COLOR=#2e8b57][b]integer[/b][/color] :: N
  [COLOR=#2e8b57][b]common[/b][/color] N [COLOR=#0000ff]! N is a global variable[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]dimension[/b][/color](N), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color] [COLOR=#2e8b57][b]out[/b][/color]) :: array
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]out[/b][/color]) :: nr_elements
  [COLOR=#2e8b57][b]integer[/b][/color] :: i, nr_lines, stat
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]30[/color]) :: dummy

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"Reading the Data from file '"[/color], fname, [COLOR=#ff00ff]"' :"[/color] 
  [COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color]fname,[COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color],[COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat)
  [COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) fname, [COLOR=#ff00ff]' cannot be opened !'[/color]
    [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]20[/color]   
  [COLOR=#804040][b]end if[/b][/color]

  [COLOR=#0000ff]! count number of lines in the file[/color]
  nr_lines [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
  [COLOR=#804040][b]do[/b][/color]
    [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]end[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]10[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]20[/b][/color]) dummy
    nr_lines [COLOR=#804040][b]=[/b][/color] nr_lines [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
  [COLOR=#804040][b]end do[/b][/color]

  [COLOR=#0000ff]! rewind back to the beginning of the file for unit=1[/color]
  [COLOR=#6a5acd]10[/color] [COLOR=#804040][b]rewind[/b][/color]([COLOR=#ff00ff]1[/color])
  [COLOR=#0000ff]! number of array elements = number of data lines in the file[/color]
  nr_elements[COLOR=#804040][b]=[/b][/color]nr_lines

  [COLOR=#0000ff]! read array elements from the file[/color]
  [COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], nr_elements
    [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]20[/b][/color]) array(i)
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(a)'[/color],[COLOR=#804040][b]advance[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'no'[/color]) [COLOR=#ff00ff]'.'[/color]
  [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#0000ff]! next line[/color]
  [COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]1[/color])

  [COLOR=#0000ff]![/color]
  [COLOR=#6a5acd]20[/color] [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'End of Reading Data'[/color]
[COLOR=#a020f0]end subroutine[/color] file2array

[COLOR=#a020f0]subroutine[/color] array2screen(array_name, array, n)
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: n, i
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]dimension[/b][/color](n), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: array
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]) :: array_name

  [COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], n
      [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) array_name, [COLOR=#ff00ff]'['[/color], i, [COLOR=#ff00ff]'] = '[/color], [COLOR=#008080]trim[/color](array(i))
  [COLOR=#804040][b]end do[/b][/color]
[COLOR=#a020f0]end subroutine[/color] array2screen
Output:
Code:
$ g95 get_files4.f90 -o get_files4

$ get_files4
 Reading the Data from file 'files.txt' :
.......
 End of Reading Data

 Number of files found:  7

 Array of files: 
 file[ 1 ] = cubes.dat
 file[ 2 ] = IN.DAT
 file[ 3 ] = inpt.dat
 file[ 4 ] = inpt_.dat
 file[ 5 ] = inpt_dos.dat
 file[ 6 ] = input.dat
 file[ 7 ] = measurement.dat
Finally, I hope that this works now with your silverfrost.
:)
 
MS Visual Fortran is actually the IVF compier. The thing I don't like about it is though you're just writing noddy program, it wants at least a core 2 duo to install!

Lots of Unis use Silverfrost because of the plato IDE and also because it has a built in windows interface so it is quite simple to knock up a simple windows/.net program. It also has a C interface. It is not fast but it is an adequate teaching tool. It does support allocatable stuff if the file extension is f95. It uses the file extension to decide which standard to follow.

Fortran at work is a funny beast. There are a lot of Fortran77 programs about and lots of people still use Fortran 77(we use 77, 95 and 2003 on site). The new stuff is Ok for writing new code but very often, you have to maintain old code. It is good that it is not still in Fortran II or Fortran66. 77 is about the oldest you'll find normally. In industry, you sometimes have to mix 2-4 variations of Fortran is the same program.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top