2015-09-01 94 views
-1

這是我的簡單代碼。 我想用打分的名字打開文件。 例如,如果a是4,那麼b是5 a要打開名稱爲9或result9.txt的文件。 可以這樣做嗎?以分數名稱打開文件

Program Dilema 

Implicit none 

Integer::a,b,c 


Write(*,'(5x,"Choose the first number: ")') 
Read(*,'(i2)')a 
Write(*,'(5x,"Choose the second number: ")') 
Read(*,'(i2)')b 

c=a+b 

Open(10,File=C,Status='Unknown') 

    write(10,*) c 

Close(10) 

Stop 
End program Dilema 

回答

2

你可以先寫你的電話號碼轉換成字符串。聲明一個字符串變量。然後在你計算你的號碼c後,你把它寫入你的字符串變量中。 以下我隨意將字符串變量的大小設置爲80,並將數字中的最大位數設置爲10,如果需要則進行調整。

character(80) :: fileName 
!... 
write(filename,'(a,I0,a)')'Score_',c,'.txt' 
Open(10,File=fileName,Status='Unknown') 

您的程序可以是這樣的

Program Dilema 

Implicit none 

    Integer::a,b,c 

    character(80) :: fileName 


    Write(*,'(5x,"Choose the first number: ")') 
    Read(*,'(i2)')a 
    Write(*,'(5x,"Choose the second number: ")') 
    Read(*,'(i2)')b 

    c=a+b 
    write(filename,'(a,I0,a)')'Score_',c,'.txt' 
    print*,fileName ! just to see what it is 

    Open(10,File=fileName,Status='Unknown') 

    write(10,*) c 

    Close(10) 

    Stop 
End program Dilema 
+0

感謝了很多。 它正在工作。 請問,你能告訴我如何打開,例如: Score_C.txt –

+1

有點乾淨的方式,'寫(文件名,'(a,I0,a)')'結果',c''。txt'' 。 ('I0'確保沒有空格) – agentp

+0

@Judge_Dred,write(filename,'(a,I0,a)')'Score _',c,'.txt',因爲agentp建議,我們不需要連接,所以我編輯答案 – innoSPG