Using variables to open output files in FORTRAN

Go To StackoverFlow.com

0

I have a piece of FORTRAN code:

C --------------------------------------------
      CALL READIN_HYD
      CALL READIN_CONFIG
      CALL READIN_FORCE
      CALL READIN_STEPPER
C --------------------------------------------

      OPEN(11,FILE='EVO_0wall.dat')

and I'm attempting to replace the hardcoded file name (EVO_0wall.dat) with something I can input from my input parameters (which are all read by the subroutines readin_hyd, etc).

I'm trying to do something like this:

  CHARACTER*30 OUTFILE

  WRITE(*,*) 'OUTPUT FILE'
  READ(*,*)   OUTFILE
  WRITE(*,*) 'OUTPUT FILE: ',OUTFILE

which I have added into the READIN_CONFIG subroutine. Coming back, I replace with

  OPEN(11,FILE=OUTFILE,STATUS='NEW')

in the main routine in the hope that it will say the same thing as before if the input file I pipe in contains 'EVO_0wall.dat' (with the apostrophes) in the appropriate place.

If I run the code, all other input variables are read correctly, and the data is output correctly - however, it creates and places the output in an odd file with no extension and broken characters for a name (for example, degree, \"{a}, and 0 ). Renaming the file with a .dat extension lets me open it, and the data within is correct. (edit: actually, the variable OUTFILE changes to the odd characters when its in the main function, if I try to simply print its value, so I guess its not just wrong syntax in the OPEN statement)

Is there some way that FORTRAN handles strings that I'm missing between these? I'm afraid I'm a novice to FORTRAN (this is someone else's code that I'm adapting), and am not quite sure what I'm missing. Any help would be much appreciated!

Thanks!

2012-04-04 02:13
by scallionpancake
How are you getting the value that you set outfile to in read_hyd back to the version of outfile in the main routine? Sorry to ask the basics, but you are aware that without the use of something like an argument list or a module they are different variables? My guess, and I stress it is a guess, is that outfile in the main routine is uninitialise - Ian Bush 2012-04-04 09:14


2

As an alternative to the suggestion of @M.S.B., you may use trim and adjustl, like this:

   implicit none
   character*99 :: outf

   outf='outf.outf'

   open(1,file=trim(adjustl(outf)))
   write(1,*)'foobar',42
   close(1)

   end

Here adjustl ensures that there's no leading whitespace, and trim trims the trailing whitespace. This should work as long as the outf variable only contains legal characters [I'd suggest using basic ASCII only].

On a side note, if I add status='new' to the open statement, it fails at runtime (with gfortran 4.4.3 at least) if the file already exists. If you really want to make sure that the existing file is not overwritten, you need to inquire first.

2012-04-05 05:20
by ev-br
+1 for trim(adjustl(...)) --- this should fix it - alexurba 2012-04-05 07:18


1

That seems OK. The following read statement should allow you to omit the apostrophes:

read (*, '(A)' )  outfile

What does the "echo" write statement of the value of outfile output?

2012-04-04 02:22
by M. S. B.


0

My FORTRAN is rusty, but I think the {a} may represent hex A, or decimal 10, which is a newline. That's what you would get when you read in the OUTFILE name.

You may need to TRIM the OUTFILE of all whitespace

2012-04-04 02:24
by jhenderson2099