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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Leading spaces in real format

Status
Not open for further replies.

halunke86

Programmer
Jun 10, 2011
1
Hello,

I have a problem with real numbers with a variable length: I want to format it that there are no leading spaces. I've read about trim(adjustl()) , but don't know how to integrate it.

Here is the fortran code:
<code>
WRITE(6,15000)(
1 '<distance>',DA(N1),'</distance>',
2 '<fstrength>',EDBA(N1),'</fstrength>',
3 '<tloss>',TL(N1),'</tloss>',N1=NA,100)
15000 FORMAT(A,F7.2,2A,F7.2,2A,F7.2,A,BN)
</code>
thanks for your help

yours sincerly

halunke86
 
Unfortunately, the solution is more complex than what you've specified. The main problem is that Fortran doesn't have any string terminators.
Code:
character*10 src
character*32 dst
src = 'ABC'
dst = trim(src)
src is still 10 characters long, dst is still 32 characters long.
Here is a possible solution to your problem. Points to note

1) toxml cannot be used in a write statement because it contains a write statement. Some compilers complain about that.
2) Use advance='no' to suppress newlines
Code:
!  --------------------------------------------------
!  Silverfrost FTN95 for Microsoft Visual Studio
!  Free Format FTN95 Source File
!  --------------------------------------------------
      character*32 function toxml(in_tag, in_fmt, in_val)
      ! The parameters
      character*(*) in_tag
      character*(*) in_fmt
      real in_val
      ! Temp for holding real to ascii
      character*8 rtoa
      
      ! Convert real to ASCII
      write(rtoa, in_fmt) in_val
      ! Create xml element
      toxml = '<' // in_tag // '>' // 
     &   trim(adjustl(rtoa)) //
     &   '</' // in_tag // '>'
      end
      
      program main
      parameter(imax=10)
      real dist(imax), fstrength(imax), tloss(imax)
      external toxml
      character*32 toxml
      character*32 xml
      
      ! put in some random values
      do ii = 1, imax
         dist(ii) = 5.273 + ii
         fstrength(ii) = 77.5 - ii
         tloss(ii) = 20.4 * ii
      enddo
      ! dump in xml
      do ii = 1, imax
         xml = toxml('distance', '(F7.2)', dist(ii))
         write(6, '(A)', advance='no')  trim(xml)
         xml = toxml('fstrength', '(F7.2)', fstrength(ii))
         write(6, '(A)', advance='no')  trim(xml)
         xml = toxml('toloss', '(F7.2)', tloss(ii))
         write(6, '(A)', advance='no')  trim(xml)
      enddo
      ! Finally, write a newline
      write (6, *)
      stop
      end
 
You could try the format F0.2 instead of F7.2

François Jacq
 
From the coding, it looks like F77. F0.x may or may not be supported. Try it first, if your compiler accepts F0.x, then use that. All current compilers accept it, even in F77 mode but on most of the older compilers, all you get is a bunch of asterisks or an error in format specification.

Also, BN is only for input - it is ignored for output.
 
I don't know how useful this is for F77 but if I want to output a number I have without any leading spaces, I just do this:

INTEGER :: MyInteger
CHARACTER(LEN=7) :: MyString

WRITE(MyString,'(i7.5)') MyInteger
WRITE(6,*) 'MyInteger: ', TRIM(ADJUSTL(MyString))



Should work for real numbers too. Obviously you have to know what the biggest value MyInteger is going to have though.
 
Just checked on MS Powerstation 4.0. Comes up with
Code:
F:\src\xml\xml.for(16): error FOR3611: illegal zero width for format field F0.2 detected between , and F0.2
Looks like the compilers which support F95 and F77 will support F0.2 but the ones prior to that won't.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top