Software Archive
Read-only legacy content
17061 Discussions

How to read non-Fortran files ? ? ?

WSinc
New Contributor I
937 Views
I wish to process a file which was NOT generated by a Fort ran application. Example: multimedia or spacecraft files such as GIF, JPG, WAV files or spacecraft telemetry files. Now obviously these must be opened via unformatted direct access statements.

The problem I have run across is that I must eventually deal with the LAST record of the file. If RECL is not an exact divisor of the file length, I am going to get a record whose length is less than RECL. For instance if the file is 11533 bytes long and RECL is 1000 bytes, the last record is 533 bytes in length. The length might be a prime number, so in that case no RECL would be an exact divisor.

After playing around with this for a while, I have come come to the tentative conclusion that the RTL does not make any distinction when reading partial records. It does not generate any special error conditions and will cheerfully read garbage into the remaining part of the I/O list. I so far have not found any way for the user to find how many bytes were actually transferred when the partial record is read. You cannot specify an EOF return on a direct access read.

By the way, the IOLENGTH specifier only gives you a count of the size of the I/O list, and cannot be used on any logical unit already open or any file name. I have looked very carefully at the write-ups for READ, OPEN and INQUIRE.

Perhaps someone has found a trick that is not documented.
0 Kudos
5 Replies
Steven_L_Intel1
Employee
937 Views
Open the file FORM='BINARY'. You can then do UNFORMATTED reads of any length, and you'll get just that number of bytes.

Steve
0 Kudos
WSinc
New Contributor I
937 Views
I understand that, but how do I test to see how many bytes are there in the last record? I could not find anything that returns the actual number of bytes that are left to be processed.
0 Kudos
Steven_L_Intel1
Employee
937 Views
How would you do this in any other language? What kind of file format makes you guess how many bytes are left? I suppose you could read one byte at a time until you got an EOF.

Steve
0 Kudos
WSinc
New Contributor I
937 Views
Well, in C++ you would use GETC to read, and PUTC to write a byte at a time. You get an EOF return when you try to run past the end of the file, because it knows how long the file is.

However, with the FORTRAN RTL, an EOF return is NOT allowed when you open with DIRECT access, so there is no way (at least not documented) that you can find out when that occurs. The RTL should know how big the file is when it's opened, so I don't see why it can't pass that info along to the user. Sure, you get an error when you read a non-existent record but that won't tell you how many bytes are in the last record that is actually there. When I was doing stuff on mainframes we had "word addressable" files with Fortran, so that took care of the problem.
0 Kudos
Steven_L_Intel1
Employee
937 Views
So don't use ACCESS='DIRECT'. Use FORM='BINARY',ACCESS='SEQUENTIAL' and pretend you're using C. A READ of a CHARACTER*1 will read one byte, a WRITE of same will write one byte.

Steve
0 Kudos
Reply