關於字符串和数字的转换問題
小弟我在編寫這個程式時,遇到關於一個bug,但小弟一直在,但卻百思不得其解。有哪位大大可以幫助小弟,告訴小弟錯在哪,並如何修正嗎?!
這是用intel fortran遇到的問題:錯誤 1 error #6514: A substring must be of type CHARACTER. 39
下面是小弟的程式碼和一些測試檔案。
program ZWD_gamit
implicit none
integer :: stat,stat2,stat3,stat4,yr,dayofyear,hr
character :: filename_ztd*12,filename_zhd*12,filename_zwd*12,first*1
real :: time_all_ztd,time_all_zhd,ztd_m,zhd_mm,ztd_mm,zwd_mm
open(11,file="list.txt")
close(11,status="delete")
open(22,file="list2.txt")
close(22,status="delete")
call system('for %f in (*_all.ztd) do echo %f >> list.txt')
call system('for %f in (*_all.zhd) do echo %f >> list2.txt')
open (11,file="list.txt")
open (22,file="list2.txt")
stat=0
stat2=0
do while (stat==0 .and. stat2==0)
read (11,"(a12)",iostat=stat) filename_ztd
read (22,"(a12)",iostat=stat2) filename_zhd
if (stat/=0 .and. stat2/=0) exit
open (33,file=filename_ztd)
open (44,file=filename_zhd)
filename_zwd=filename_ztd(1:8)//".zwd"
open (55,file=filename_zwd)
stat3=0
do while (stat3==0)
read (33,50,iostat=stat3) time_all_ztd,ztd_m,yr,dayofyear,hr
50 format (f12.7,4x,f6.1,3x,i4,3x,i3.3,3x,i2.2)
if (stat3/=0) exit
stat4=0
do while (stat4==0)
read (44,100,iostat=stat4) time_all_zhd,zhd_mm
100 format (f12.7,4x,f6.1)
if (stat4/=0) exit
if (time_all_ztd==time_all_zhd) then
read (zhd_mm(1:1),"(a1)") first
if( first/="*") then
ztd_mm = ztd_m*1000D0
zwd_mm = ztd_mm - zhd_mm
write (55,150) time_all_ztd,zwd_mm,yr,dayofyear,hr
150 format (f12.7,3x,f12.7,3x,i4,3x,i3.3,3x,i2.2)
end if
end if
end do
rewind(44)
end do
close(33)
close(44)
close(55)
end do
close(11)
close(22)
end program
确定一下 zhd_mm 应该是 real 类型,还是 character 类型?
你定义为 real 类型
但 read (zhd_mm(1:1),"(a1)") first 这种写法要求它是 character 类型 是的,因為 zhd_mm的值,有些是有數值,有些沒有數值,有數值會以浮點數呈現,沒有數值會以*******呈現
所以我才會想說用read (zhd_mm(1:1),"(a1)") first 把 zhd_mm的第一個字元用first的來代替
可以用來判斷是否為有數值,若沒有數值就可以不必寫入55。 如果没有数值,是 **** 的话,那么 stat4 就不会是 0,就会 exit 了。 楚香饭 发表于 2014-10-16 12:17
如果没有数值,是 **** 的话,那么 stat4 就不会是 0,就会 exit 了。
所以說我必須寫個程式把*****替代成別的符號,然後這個迴圈才能繼續下去嗎?!
還是說還有其他可行的方法也可達到我想要的目的呢?! character( len = 30 ) c_zhd_mm !// c_zhd_mm 定义为字符型
do while (stat4==0)
read (44,*,iostat=stat4) time_all_zhd,c_zhd_mm !// 通常 * 就足够 read 了
if (stat4/=0) exit
if (time_all_ztd==time_all_zhd) then
!read (zhd_mm(1:1),"(a1)") first !//这句去掉
if( c_zhd_mm(1:1) /= "*") then
read( c_zhd_mm , * ) zhd_mm !// 如果有数值,再从 c_zhd_mm 中读取 zhd_mm
ztd_mm = ztd_m*1000D0
zwd_mm = ztd_mm - zhd_mm 楚香饭 发表于 2014-10-16 17:56
character( len = 30 ) c_zhd_mm !// c_zhd_mm 定义为字符型
do while (stat4== ...
所以說read他可以轉換變數類型,從character轉換成real(浮點數),但卻沒辦法從real轉換成character,是這個意思嗎? 从 real 转换成 character 用 write 既可
write( character , * ) real 楚香饭 发表于 2014-10-17 08:12
从 real 转换成 character 用 write 既可
write( character , * ) real
从 real 转换成 character 用 write 既可
write( character , * ) real
這個write是把character 轉換成 real 嗎?
可是我的問題是說,能不能使用read指令在程式中途,把資料由real類型轉換成character類型?
所以答案是不行的嗎!?
我不是很清楚這個解釋,能否再講詳細一點呢!?感謝您 read 只能把 character 转换为 real(或 integer 或 complex)
write 只能把 real(或 integer 或 complex)转换成 character
你要做什么转换,就用什么语句。不要强求 read 非要倒过来转换。
喝水用杯子,钉钉子用榔头,你非要问能不能用榔头来喝水,那只能说不行。
页:
[1]
2