user1949518
user1949518

Reputation: 1

Read and print with FORTRAN

I need to write a Fortran program which can read and print a .dat file.

(A file homework_6.dat contains book records: Name (up to 25 characters), publish year (4 digit integer), price (6 digit real), ISBN (13 digit integer). Write a program to read the file (homework_6.dat) and print out (on screen or into another file) the details in the following format:

                                     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)

Here what i write

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

But Fortran says there is an error at line 8

I don't know what to do :(

Sonya (ITU)

--edit--

So i tried to write a program, but i still have error after the execution

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

I have an error in line 14. ERROR 52, invalid character in field DAT - in file homework.f95 at line 14 [+01b3]

Upvotes: 0

Views: 4526

Answers (2)

maxm
maxm

Reputation: 113

Replace unit=7 by 7, in line 8. But the problem is you are reading from homework_6.dat and writing in the same file. You can open new file or output on the screen. I have modified your code to read from the data file you have shown assuming it has 3 lines of heading before the data.

    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

Your hw problem can easily be solved by consulting any beginners fortran book.

Upvotes: 0

You are reading with the list directed format, but this will not work. There are spaces in the book names and the compiler is not going to find, where it ends, and where the year begins.

You must use a format. A tip: use a format string in the read statement, not a format statement with a label. The format will be similar to the output one you have.

Another hint, your output format for price is too short. I recommend f6.2.

Upvotes: 1

Related Questions