我试图在Fortran 95中编写代码,从文件中读取字符串,然后从文件中打印字母频率表。该文件的文本如下例所示,每行不超过128个字符:
Ablblb lbla sdrtwfwefw
Waerfaw efeafawef awef
Pefwae fwefawefw efawe
Fcicnj ioejo, o njcdid
Pweko jai, wadwed awdd
所以我做了一个对第一个字母(A或a)工作正常的程序,但是计数器不能用于其余的字母并且总是打印" 0"。这就是我所做的:
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
第一个count
工作正常(字母A或a),但它打印" 0"其余的字母。 DO循环是否有问题没有正确地重置变量count
?