File processing in Fortran 90


manjurul
02-03-2011, 11:25 AM
Hi everyone,

I need an example of a program which is for file processing. I have tried the following program, but it gave me error. Please help me..




program Direct_Access_File_Demo

implicit none
integer :: RecordLenght
integer :: OpenStatus, InputStatus, PartNumber
character(20) :: FileName
character(31) :: PartsRecord(31)

write (*, '(1X, A)', ADVANCE = "NO") "Enter name of file: "
read *, FileName

! RecordLength = 31

open (UNIT = 10, FILE = FileName, STATUS = "OLD", &
ACCESS = "DIRECT", ACTION = "READ", POSITION = "REWIND", &
FORM = "FORMATTED", RECL = 31, IOSTAT = OpenStatus)
if (OpenStatus > 0) stop " *** Cannot open file *** "

do
write (*, '(1X, A)', ADVANCE = "NO") &
"Enter part number (0 to stop): "
read *, PartNumber

if (PartNumber == 0) exit
read (UNIT = 10, FMT = '(A)', REC = PartNumber, &
IOSTAT = InputStatus) PartsRecord
if (InputStatus == 0) then
print '(1X, "Part", I3, ": ", A)', PartNumber, PartsRecord
else
print '(1X, "Invalid part number: ", I3)', PartNumber
end if
end do
close(10)
end program Direct_Access_File_Demo

davekw7x
02-04-2011, 09:40 AM
...error....
The main problem that I have with your post is that it creates questions that make it kind of hard to help without getting some answers from you:

What compiler are you using? What error are you getting? Where did you get the data file that you are trying to read? Can you post a file with a few records so that we might have a ghost of a chance of testing?

Anyhow, I'll give it a shot:

I see a couple of problems.

1. I perceive that your records fields are supposed to be 31 characters wide.
If that is the case, then try this:
Change your PartsRecord declaration from this:

character(31) :: PartsRecord(31)


to the following:

character(31) :: PartsRecord


2. When you are going to open a file for reading with direct access, you should not (can not) give it a 'POSITION=' in the open statement. The whole point of direct access is that is no "position" pointer for the file that tells the program where the "next" record is going to be accessed from. There is no "next" record. Each read or write statement for a direct access file is for a specific record number.

So try this:
Change your open statement from this:

open (UNIT = 10, FILE = FileName, STATUS = "OLD", &
ACCESS = "DIRECT", ACTION = "READ", POSITION = "REWIND", &
FORM = "FORMATTED", RECL = 31, IOSTAT = OpenStatus)


to the following:

open (UNIT = 10, FILE = FileName, STATUS = "OLD", &
ACCESS = "DIRECT", ACTION = "READ", &
FORM = "FORMATTED", RECL = 31, IOSTAT = OpenStatus)


Now, as for the data file, I have attached a sample.

I made the two modsifications to your code and compiled with GNU gfortran.

Here's a run, using the attached file:

Enter name of file: data.txt
Enter part number (0 to stop): 4
Part 4: <Begin record 4 End>
Enter part number (0 to stop): 1
Part 1: <Begin record 1 End>
Enter part number (0 to stop): 7
Part 7: <Begin record 7 End>
Enter part number (0 to stop): 10
Invalid part number: 10
Enter part number (0 to stop): 8
Invalid part number: 8
Enter part number (0 to stop): 7
Part 7: <Begin record 7 End>
Enter part number (0 to stop): 0



Regards,

Dave