2017-09-10 86 views
-1

array這是我的代碼:讀1D和2D從文件

Program Reading_from_file 

Implicit none 

Integer::i,j 
Integer,dimension(3)::Type_SNDM 
Integer,dimension(2,3)::Type_NNDM 
Real,dimension(3)::Lenght_SNDM 
Real,dimension(2,3)::Lenght_NNDM 
Character(350),parameter::fmtA_1_5='(2x,3(f4.2,2x,i1,3x))' 
Character(350),parameter::fmtA_1_6='(/,/,/,2x,4(f5.3,2x,i1,3x))' 

Open(Unit=15,File='Input.txt',Status='Unknown',Action='Readwrite') 

    Read(15,trim(adjustl(fmtA_1_5)))(Lenght_SNDM(j),Type_SNDM(j),j=1,3) 

    Do concurrent(i=1:2) 

     Read(15,trim(adjustl(fmtA_1_6)))(Lenght_NNDM(i,j),Type_NNDM(i,j),j=1,3) 

    End Do 

Close(Unit=15,Status='Keep') 

Open(Unit=15,File='Output.txt',Status='Unknown',Action='Readwrite') 

    Write(15,trim(adjustl(fmtA_1_5)))(Lenght_SNDM(j),Type_SNDM(j),j=1,3) 

    Do concurrent(i=1:2) 

     Write(15,trim(adjustl(fmtA_1_6)))(Lenght_NNDM(i,j),Type_NNDM(i,j),j=1,3) 

    End Do 

Close(Unit=15,Status='Keep') 

End Program Reading_from_file 

這是Input.txt內容:

|2.50||2| |2.50||2| |2.50||2| 


|0.250||3| |0.250||3| |0.250||3| 
|0.250||3| |0.250||3| |0.250||3| 

我特林從Input.txt文件中讀取一些值。接下來的目的是在Output.txt文件中寫入該值。 Afther程序執行的消息是:Fortan runtime error: End of file。 代碼或閱讀格式有什麼問題?

+0

你的'Input.txt'中的第一個字符缺失,我猜它應該是一個空格,這只是一個轉錄錯誤。你爲什麼使用併發做I/O? – Ross

回答

1

您的格式fmtA_1_6嘗試跳過3行/,/,/,,這(我猜)是爲了跳過第1行和第4行之間的兩個空白行。循環中的第二次讀取(i = 2)也嘗試空白行。這些行不存在,你會得到EOF錯誤。

在進入do循環之前,您應該跳過兩個空行。此外,您應該而不是使用concurrent do進行I/O操作。

添加這些更改並修復Input.txt文件中的轉錄錯誤後,代碼似乎可以正常工作。中讀取的部分現在是:

Read(15,trim(adjustl(fmtA_1_5)))(Lenght_SNDM(j),Type_SNDM(j),j=1,3) 

! -- Skip 2 lines 
read(15,*) 
read(15,*) 

Do i=1,2 

    Read(15,trim(adjustl(fmtA_1_6)))(Lenght_NNDM(i,j),Type_NNDM(i,j),j=1,3) 

End Do 

的格式:

Character(350),parameter::fmtA_1_6='(2x,4(f5.3,2x,i1,3x))' 

想必你也應該寫在該節兩個空行。