Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Getting write statement to write to unit or string

Adrian_F_1
Beginner
406 Views

I have a very large routine full of WRITE statements which write to a unit number (for file output).  This routine is now going to also be called by a GUI through a DLL, and in this case I want the WRITE statements to write to a character array instead of the file unit number.  (It must still be able to write to the unit number if not called from the GUI).  Now short of putting an IF statement around every WRITE statement, can I use a better method eg. using pointers to do it?

Say I have:

WRITE(IU,'(a,/,a)') 'test1', 'test2'

And I want the same code to work if IU points to a character array (if called from the GUI)

So I know I can say:

INTEGER :: IU

CHARACTER(256) :: CA(2)

IF(GUI) then

  WRITE(CA(1:2),'(a,/,a)') 'test1', 'test2'

ELSE

  WRITE(IU,'(a,/,a)') 'test1', 'test2'

ENDIF

but I'd prefer it if I can use then same code without the IF test, by some fancy footwork on IU and CA(2) beforehand so I wouln't have to modify every IF statement, eg.

IF(GUI) THEN

  IU -> CA   ! or whatever

ENDIF

0 Kudos
3 Replies
Adrian_F_1
Beginner
406 Views

Actually I'll just change IU into a character array, and then write the character aray into the file at the end of the routine!

0 Kudos
Michael_Roberts
New Contributor I
406 Views

Hi Adrian,

Could you consider always writing to the character array, then at the end of the routine check if the GUI flag is set, and if not loop through the character array and write to the file? 

Michael

0 Kudos
Adrian_F_1
Beginner
406 Views

Hi Michael, thanks that's what I did in the end.

0 Kudos
Reply