2013-01-04 46 views
0

我需要編寫一個可讀取並打印.dat文件的Fortran程序。使用FORTRAN讀取並打印

(一個文件homework_6.dat包含書籍記錄:名稱(最多25個字符),發佈年份(4位整數),價格(6位數字實數),ISBN(13位整數)。文件(homework_6.dat),並以下列格式打印出來(在屏幕上或到另一個文件)的詳細信息:

         Publish 
      Name      Year ($)  ISBN 
     -------------------------  ---- ------  ------------- 
     Principles of Combustion  2005 107.61  9780471046899 
     An Introduction to Comb  2011 193.99  9780073380193 
     Guide to Fortran    2009  71.95  9781848825420 
     Modern Fortran Explain   2011 100.00  9780199601417 
     Introduction to Program  2012 200.00  9780857292322) 

這裏我寫的東西

program dat 
implicit none 
character (len=25) :: Name 
integer :: i, publish_year, ISBN 
real :: price 
open(unit=7, file="homework_6.dat", status="old", action="readwrite") 
do i=1, 10 
read (unit=7,fmt="(a25,i4,f3.2,i13)") Name, publish_year, price, ISBN 
write (unit=7,fmt="(a25,i4,f3.2,i13)") Name, publish_year, price, ISBN 
end do 
close(unit=7) 
end program dat 

但Fortran語言說,有是一個錯誤第8行

,我不知道該怎麼辦:(

索尼婭(ITU)

- 編輯 -

於是,我寫了一個程序,但執行後,我仍然有錯誤

program dat 
    implicit none 
    character (len=25) :: Name 
    character (len=13) :: ISBN 
    integer :: i, publish_year 
    real :: price 
    open(unit=10, file="homework_6.dat", status="old", action="readwrite") 
    open(unit=11, file="output_hw6.dat") 
    !Comment: these below 3 lines are for skipping 3 heading your input 
    read(10,*) 
    read(10,*) 
    read(10,*) 
    do i=1, 10 
    read (10,*) Name, publish_year, price, ISBN 
    write (11,1) Name, publish_year, price, ISBN 
    1 format(a25,2x,i4,2x,f3.2,2x,a13) 
    end do 
    close(unit=10) 
    end program dat 

我在線路錯誤14. ERROR 52,在字段無效字符 DAT - 在文件homework.f95在管線14 [+ 01b3]

+0

您應該發佈您看到的錯誤消息。 –

+1

什麼錯誤?運行時還是編譯時?寫在這裏。 –

+1

您需要執行三次讀取才能跳過文件中的三個標題行。 –

回答

0

在第8行用7替換單元= 7。但問題是你正在從homework_6.dat讀取並寫入同一個文件。您可以在屏幕上打開新文件或輸出。我修改了您的代碼,以便從您顯示的數據文件中讀取數據,前提是數據前有3行標題。

program dat 
    implicit none 
    character (len=25) :: Name 
    integer :: i, publish_year, ISBN 
    real :: price 
    open(unit=7, file="homework_6.dat", status="old", action="readwrite") 
    open(unit=8, file="output_hw6.dat") 
    !Comment: these below 3 lines are for skipping 3 heading your input     
    read(7,*) 
    read(7,*) 
    read(7,*) 
    do i=1, 10 
    read (7,*) Name, publish_year, price, ISBN 
    write (8,1) Name, publish_year, price, ISBN 
    1 format(a25,2x,i4,2x,f3.2,2x,i13) 
    end do 
    close(unit=7) 
    end program dat 

您可以通過諮詢任何初學者fortran書輕鬆解決您的問題。

+0

我明白爲什麼我需要使用3次讀取(跳過3行) 但我有這個錯誤 錯誤283 - DUMMY必須出現在類型聲明中,因爲IMPLICIT NONE已被使用 編譯失敗。 – user1949518

+0

當我把虛擬字符,我仍然有一個錯誤 DAT - 在文件freeformat4.f95在14行[+0204] – user1949518

+0

是的我應該給虛擬變量一個類型。您可以刪除虛擬變量,只需將行保留爲已讀(7,*),確保您在開始讀取實際數據之前跳過數據文件中所需的行,否則在讀取時仍會出錯。 – maxm

1

您正在閱讀的是列表定向格式,但這不起作用。書名中有空格,編譯器不會找到,它的結束位置以及年份開始的地方。

您必須使用格式。提示:在讀取語句中使用格式字符串,而不是帶有標籤的格式語句。格式將與您擁有的輸出類似。

另一個提示,您的價格輸出格式太短。我推薦f6.2。

+0

Humm ..好吧,我需要使用「字符串」,但我不知道如何 – user1949518

+0

這不是重點。而不是標籤,你可以直接把格式作爲'read(*,'(f5.1)')',但這相當於要格式化的標籤,它不會解決你的程序。你必須找到正確的格式。 –

+1

我能夠運行它,但我現在不打算髮布確切的解決方案。 –

相關問題