0

This is my code:

Program String_Triming

Implicit none

Open(15, File = 'Output.txt')

Write(15,'(A,1x,"j",1x,A)') Ispis(20.45),Ispis(20.45)
Write(15,'(A,1x,"j",1x,A)') Ispis(-20.45),Ispis(-20.45)

Close(15)

Contains

Function Ispis ( Deg ) result ( Str )

  Real,intent(in)::Deg
  Character(len=16):: Str

  If ( Deg > 0 ) then

     Write(Str,'(F0.3)') 1000.0 + Deg
     Str = Str(2:)

     Else

     Write(Str,'(F8.3)') 1000.0 + abs(Deg)
     Write(Str,'("-",A)') Str(3:)

  End If

End Function Ispis

End program String_Triming

The content of Output.txt file is:

020.450          j 020.450         
-20.450          j -20.450

The result I want to get from this code is:

020.450 j 020.450         
-20.450 j -20.450

How do I get that result? Is there way to trim the length of Str to Len=8 which is the length of 020.450?

francescalus
  • 30,576
  • 16
  • 61
  • 96
  • 1
    You say you want to trim `str` to length of 8, so why don't you declare the function result to be of that length rather than 16? – francescalus Jan 03 '18 at 03:00
  • 1
    A statement such as `Write(Str,'("-",A)') Str(3:)` is not valid Fortran: it isn't allowed to have an output item (`str(3:)`) in the internal file (`str`). Fortunately, you can rewrite this as `str="-"//str(3:)`. – francescalus Jan 03 '18 at 03:04
  • Isn't this just an updated version of https://stackoverflow.com/questions/48066269/floating-point-output-format-with-leading-zeros ? – High Performance Mark Jan 03 '18 at 07:49
  • @HighPerformanceMark It could be! –  Jan 03 '18 at 11:09
  • @francescalus I do not now why you ask me that question because i can not understand what is not clear what i want to do with this function? Just to explain again clearly, so i want to get, for a output writing, this form of result for a A descriptor: `000.000` for any Deg (positive or negative). If you make declaration `character(8) = str` you will get this result: `000.000 ` (adding one blank space!!! - I do not want that one blank space in my result for a A editor in format for writing to `Output.txt`. –  Jan 03 '18 at 11:22
  • @francescalus If you can teach me how to do that i will make a function for any form of result, for example, this: `0000000.00000` (Len = 13) –  Jan 03 '18 at 11:24
  • Do you really need the leading zeros, or do you just care about having a constant width for each column? I've seen your previous (related) question and I see that you want to include the possibility for printing the "-" sign for negative numbers. – Matt P Jan 03 '18 at 23:36
  • @MattP I have a output file for a every single iteration in calculation process. In that file there is also a lot others result. For example, in my code I have a 21 iterations and I want to avoid 21 definitions for writing format. I know which values I can expect so I want to make one definition for writing format, which can be: `0000.00` For example, result are: `2.34, 23.45, -46.78,123.98` –  Jan 03 '18 at 23:42

2 Answers2

0

It's not quite clear what you want. If all you want is to remove the spaces from the output file, why not just run it through sed instead of writing a Fortran Program:

$ cat Output.txt 
020.450          j 020.450
-20.450          j -20.450
$ sed -r 's/ +/ /g' Output.txt 
020.450 j 020.450
-20.450 j -20.450

If you want to produce output like this in the first place, you could 'overwrite' the first three characters of str with an integer format. Something like this:

function Ispis(Deg) result(Str)
    real, intent(in) :: Deg
    character(len=7) :: Str
    write(Str, '(F7.3)') Deg
    if ( Deg < 0 ) then
        write(Str(:3), '(I3.2)') int(Deg)
    else
        write(Str(:3), '(I3.3)') int(Deg)
    end if
end function Ispis

note: the length of 020.450 is 7, not 8.

chw21
  • 7,970
  • 1
  • 16
  • 31
  • Result of calculation: `2.45 , 45.78 , -134.89` I want to write results in this form: `0002.45, 0045.78, -134.89` I have a 21 iterations and i want to use one format for all itsrations and that is: `0000.00` The lenght of that format is 7. I want to use strings for a writing results - strings with same lengs. Is it cleRly now? –  Jan 04 '18 at 00:32
0

This is the solution for getting wanted result:

Program Main

Implicit none

Open(15,File='Output.txt')

  Write(15,'(1x,a,1x,"j",1x,a,1x,"Juhu!")') Writing_01(67.45),Writing_01(-4.04)
  Write(15,'(1x,a,1x,"j",1x,a,1x,"Juhu!")') Writing_02(67.45),Writing_02(-4.04)

Close(15)

Contains

Function Writing_01 ( Deg ) Result ( Str )

Real,intent(in) :: Deg
Character(:),allocatable :: Str
Character(len = 15 ) :: Str_temp

If ( int( Deg ) > 0 ) then

    Write(Str_temp , '(F0.2)' ) 100000.0 + Deg
    Str_temp = Str_temp(2:)

Else

    Write(Str_temp, '(F0.2)' ) 100000.0 + abs(Deg)
    Str_temp = "-"//Str_temp(3:)

Endif

Str = trim ( adjustl ( Str_temp ))

End Function Writing_01

Function Writing_02 ( Deg ) Result ( Str_temp )

Real,intent(in) :: Deg
Character(:),allocatable :: Str_temp
Character(len=1561) :: Form_02 , Res

If (int( Deg ) > 0 ) then

  Form_02 = '(i5.5,f0.2)'   ! allow a total of 4 leading zeros.

Else

  Form_02 = '(i5.4,f0.2)'   ! "-" sign takes up one space, so 3 leading zeros remain.

Endif

Write(Res , Form_02 )  int( Deg ), abs( Deg - int( Deg ) )

Str_temp = trim ( adjustl ( Res ))

End Function Writing_02

End program Main