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!

1
  • 3
    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 uninitialised Commented Apr 4, 2012 at 9:14

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.