I have the following Fortran 95 code:
program write_test
CHARACTER(LEN=3) :: str
write(*,*) (' ',"""",'File'//trim(str(i)),"""",' ',"""",'Frequency'//trim(str(i)),"""",i=1,5)
end program write_test
!character*(*) function str(k)
character(*) function str(k)
! "Convert an integer to string."
integer, intent(in) :: k
write (str, *) k
str = adjustl(str)
end function str
When I compile and run it, I get the following output:
" File1" " Frequency1" " File2" " Frequency2" " File3" " Frequency3" " File4" " Frequency4" " File5" " Frequency5"
Why is there a space between the double quote and the letter F?
For list-directed output with the * "format", the compiler has freedom to insert one or more spaces between the items printed. For more precise control, use a format string, as in the code below, tested with g95 and gfortran. You can use the i0 format to print an integer without spaces.
program write_test
character(len=3) :: str
write (*,"(100a)") (" ","""","File"//trim(str(i)),"""", &
" ","""","Frequency"//trim(str(i)),"""",i=1,5)
! preferred way is line below
write (*,"(100(1x,2a,i0,a))") ("""","File",i,"""","""","Frequency",i,"""",i=1,5)
end program write_test
character(*) function str(k)
! Convert an integer to string
integer, intent(in) :: k
write (str,"(i0)") k
str = adjustl(str)
end function str
Related
I'm trying to write a code in Fortran 95 that read strings from a file and then print a frequency table of letters from the file. The file has a text like the example below, with no more than 128 characters per line:
Ablblb lbla sdrtwfwefw
Waerfaw efeafawef awef
Pefwae fwefawefw efawe
Fcicnj ioejo, o njcdid
Pweko jai, wadwed awdd
So I did one program that is working fine for the first letter (A or a), but the counter doesn't work for the rest of the letters and always print "0". This is what I did:
program read_file
implicit none
integer :: count, i, j, k, n
character(len=80) :: arch
character(len=128) :: line
character :: c, d
logical :: rexist
print *,'Name of the file (example: "text1.txt"): '
read *,arch
count=0
inquire(file=arch, exist=rexist)
if(rexist) then
open(unit=10, file=arch)
j=97
do i=65,90
c=achar(i)
d=achar(j)
do
read(unit=10, fmt='(A128)', end=999)line
n=len_trim(line)
do k=1,n
if(line(k:k)==c .or. line(k:k)==d)then
count=count+1
end if
end do
cycle
999 exit
end do
print *, "Letter ", c, " or ", d, " Total: ", count
j=j+1
count=0
end do
close(unit=10)
else
print *,"Invalid file!"
end if
end program read_file
The first count works fine (letter A or a) but it prints "0" for the rest of the letters. Is there something wrong with the DO loop that doesn't reset the variable count properly?
How do I invoke a program and pass it standard input? Hypothetical example in Bash:
(echo abc; echo abba) | tr b B
Note that:
I don't have the input in a string (I'm generating it as I iterate)
I don't know how long input is
The input may span multiple lines, as in this example
I've written this in 19 other languages already, the way I usually approach it is to get a file descriptor for the program's standard input, and then write to the file descriptor the same way I would write to standard output.
What I've tried so far: Based on Invoke external program and pass arguments I tried passing it to echo and using the shell to handle the piping. This doesn't work if my input has single quotes in it, and it doesn't work if I don't have my input in a string (which I don't)
Here is my code, currently trying to pull it off by calculating the string that will be printed (it fails right now).
As for single quotation, how about inserting the escape character (\) before each quotation mark (')...? It seems to be working somehow for the "minimal" example:
module utils
implicit none
contains
function esc( inp ) result( ret )
character(*), intent(in) :: inp
character(:), allocatable :: ret
integer :: i
ret = ""
do i = 1, len_trim( inp )
if ( inp( i:i ) == "'" ) then
ret = ret // "\'"
else
ret = ret // inp( i:i )
endif
enddo
endfunction
endmodule
program test
use utils
implicit none
character(100) :: str1, str2
integer i
call getcwd( str1 ) !! to test a directory name containing single quotes
str2 = "ab'ba"
print *, "trim(str1) = ", trim( str1 )
print *, "trim(str2) = ", trim( str2 )
print *
print *, "esc(str1) = ", esc( str1 )
print *, "esc(str2) = ", esc( str2 )
print *
print *, "using raw str1:"
call system( "echo " // trim(str1) // " | tr b B" )
print *
print *, "using raw str2:"
call system( "echo " // trim(str2) // " | tr b B" ) !! error
print *
print *, "using esc( str1 ):"
call system( "echo " // esc(str1) // " | tr b B" )
print *
print *, "using esc( str2 ):"
call system( "echo " // esc(str2) // " | tr b B" )
print *
print *, "using esc( str1 ) and esc( str2 ):"
call system( "(echo " // esc(str1) // "; echo " // esc(str2) // ") | tr b B" )
! pbcopy (copy to pasteboard; mac-only)
! call system( "(echo " // esc(str1) // "; echo " // esc(str2) // ") | pbcopy" )
endprogram
If we run the above program in the directory /foo/baa/x\'b\'z, we obtain
trim(str1) = /foo/baa/x'b'y
trim(str2) = ab'ba
esc(str1) = /foo/baa/x\'b\'y
esc(str2) = ab\'ba
using raw str1:
/foo/baa/xBy
using raw str2:
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
using esc( str1 ):
/foo/baa/x'B'y
using esc( str2 ):
aB'Ba
using esc( str1 ) and esc( str2 ):
/foo/baa/x'B'y
aB'Ba
This is what I mean by a minimal example.
call execute_command_line("(echo ""hexxo world"";echo abba)|tr x l")
end
hello world
abba
Is this not doing exactly what you ask, invoking tr and passing standard input?
I have the following code:
program main
character (len=15) :: abc = "te st tex t"
print *, trim(abc)
end program main
Which outputs:
te st tex t
I excepted all the whitespace to be removed but it wasn't. How can I remove all the whitespace from the string?
Trim will remove spaces only at the edges, not in the middle (this is common behaviour on almost all languages/libraries). If you want to remove all spaces in the string, you will have to create your own function to do this, iterating through the string.
Ex.:
program Test
implicit none
! Variables
character(len=200) :: string
! Body of Test
string = 'Hello World 7 9'
print *, string
call StripSpaces (string)
print *, string
contains
subroutine StripSpaces(string)
character(len=*) :: string
integer :: stringLen
integer :: last, actual
stringLen = len (string)
last = 1
actual = 1
do while (actual < stringLen)
if (string(last:last) == ' ') then
actual = actual + 1
string(last:last) = string(actual:actual)
string(actual:actual) = ' '
else
last = last + 1
if (actual < last) &
actual = last
endif
end do
end subroutine
end program Test
This was tested on intel compiler, not on gfortran, but I think it will work.
I was able to do this using the variable string library described here ( http://schonfelder.co.uk/is1539-2-99.htm ). The source code link is found in the introduction section of the ISO document.
Here is the code
program Console1
use ISO_VARYING_STRING
implicit none
! Body of Console1
character(LEN=50) :: text = 'Hello World John Mary '
character(LEN=50) :: res
print *, trim(text)
! 'Hello World John Mary'
res = REPLACE(text,' ','', every=.TRUE.)
print *, trim(res)
! 'HelloWorldJohnMary'
end program Console1
Here's a dirty, shameful way to eliminate the spaces. This is only likely to work if a compiler lays out a length-15 string in the same order and space as it would a 15-element array of characters. While this is likely to be true, and in my recent experience is true, it is not guaranteed to be so by the standard. That aside, this approach may be good enough.
! declarations
CHARACTER (len=15) :: abc = "te st tex t"
CHARACTER, DIMENSION(LEN(abc)) :: abc_array
! or CHARACTER, DIMENSION(:), ALLOCATABLE :: abc_array if your compiler supports
! automatic allocation
! transfer the string into an array of characters
abc_array = TRANSFER(abc,abc_array)
! eliminate the spaces, and transfer back to the string
abc = TRANSFER(PACK(abc_array,abc_array/=' '),abc)
! now all the spaces are at the end of abc so the following statement writes the
! string with no spaces
WRITE(*,*) TRIM(abc)
Use this approach at your own risk.
For those averse to TRANSFER perhaps a nice little recursive function would appeal. As written this depends on Fortran 2003's ability to automatically allocate character scalars, but it shouldn't be too hard to modify if your compiler doesn't support this feature yet.
RECURSIVE FUNCTION stripper(string,ch) RESULT(stripped)
CHARACTER(len=*), INTENT(in) :: string
CHARACTER, INTENT(in) :: ch
CHARACTER(:), ALLOCATABLE :: stripped
IF (LEN(string)==1) THEN
IF (string==ch) THEN
stripped = ''
ELSE
stripped = string
END IF
ELSE
IF (string(1:1)==ch) THEN
stripped = stripper(string(2:),ch)
ELSE
stripped = string(1:1)//stripper(string(2:),ch)
END IF
END IF
END FUNCTION stripper
You can try this:
program test
!erase blank space in a string
!run over every character of the string and just take every non-blank in other variable.
implicit none
character (len=100) str1,str2
integer i
str2='' !in this variable will be save non-blank spaces
str1=' a b c de ' !Test string with blank spaces
write(*,*)len_trim(str1), str1
do i=1,len(str1)
if (str1(i:i).ne.' ')str2=trim(str2)//trim(str1(i:i))
end do
write(*,*)len_trim(str2), str2
end
I need to read this kind of file. I have problem reading the delimiters within the file.
xxxx
dd/mm/yyyy
text
text
angle
Number of points
-13.82|654781.695|292510.337|4.889|SD
0.00|654795.515|292510.337|4.373|P1
1.25|654796.765|292510.337|4.324|SD
1.29|654796.805|292510.337|4.657|SD
1.68|654797.195|292510.337|4.622|SD
......
(1) Read lines from the file into a string using the "(a)" format.
(2) Replace unwanted delimiters in the string with delimiters recognized by Fortran (spaces or commas).
(3) Get data from the string using an "internal read".
The program below illustrates steps (2) and (3).
program main
implicit none
character (len=20) :: str
integer :: i
real :: x,y
str = "321.1|5678.9"
do i=1,len_trim(str)
if (str(i:i) == "|") str(i:i) = " "
end do
print*,"str = '" // trim(str) // "'" ! output: '321.1 5678.9'
read (str,*) x,y
print*,"x, y =",x,y ! output: x, y = 321.1 5678.9
end program main
I would like to have some clarifications about the way Fortran handles "empty" characters in strings.
Let us assume we have this situation:
program main
implicit none
test('AB')
end program
where
function test(name)
implicit none
character(10) :: name
character(3) :: cutname
write(*,*) '-'//name//'-' ! Gives output "-AB -"
! Space has then been added at the end
cutname(1:3) = name(1:3)
write(*,*) '1-'//cutname//'-' ! Gives output "-AB -"
! It seems there is a space then at the end
! of cutname
write(*,*) (cutname(1:2) == 'AB') ! Gives output T (true)
write(*,*) (cutname(3:3) == ' ') ! Gives output F (false)
write(*,*) (cutname == 'AB ') ! Gives output F (false)
end function
I am pretty curious about what is happening in this case.
Thanks in advance.
Standard strings in Fortran are fixed length. If you don't use the entire string, they are padded on the end with blanks/spaces.
I altered your example program to pass compiler checks of gfortran and ifort. Your function had no return, so better as a subroutine. The compilers noticed the inconsistency between the lengths of the actual and dummy argument -- because I put the procedure into a module and useed it so that the compiler could check argument consistency. They complain about passing a length 2 string to a length 10 string. How are the remaining characters supposed to be defined?
module test_mod
contains
subroutine test(name)
implicit none
character(10) :: name
character(3) :: cutname
write(*,*) '-'//name//'-' ! Gives output "-AB -"
! Space has then been added at the end
cutname(1:3) = name(1:3)
write(*,*) '1-'//cutname//'-' ! Gives output "-AB -"
! It seems there is a space then at the end
! of cutname
write(*,*) (cutname(1:2) == 'AB') ! Gives output T (true)
write(*,*) (cutname(3:3) == ' ') ! Gives output F (false)
write(*,*) (cutname == 'AB ') ! Gives output F (false)
end subroutine test
end module test_mod
program main
use test_mod
implicit none
call test('AB ')
end program
When I run this version, the outputs are T, T and T, which is what I expect.
EDIT: I suggest using full warning and error checking options of your compiler. That is how I quickly found the issues with the example. With gfortran: -O2 -fimplicit-none -Wall -Wline-truncation -Wcharacter-truncation -Wsurprising -Waliasing -Wimplicit-interface -Wunused-parameter -fwhole-file -fcheck=all -std=f2008 -pedantic -fbacktrace.
A string assignment statement doesn't require the two sides to have the same lengths. If the RHS is shorter than the string variable on the LHS, it will get padded on the end with blanks. Here, the arguments should be consistent, including in length.