Re: Fortran 77 - File reading problem



Louis Krupp wrote:
holysword@xxxxxxxxx wrote:
On 22 out, 19:04, Louis Krupp <lkr...@xxxxxxxxxxxxxxxxxxxxxxx> wrote:
holysw...@xxxxxxxxx wrote:
On 22 out, 17:46, nos...@xxxxxxxxxxxxx (Richard Maine) wrote:
<holysw...@xxxxxxxxx> wrote:
Sry. When I change the FORM I got no error at opening, But I got
IOSTAT=31 at reading.
You can't just change the OPEN without also changing the READ. Thay have
to match. That's what the error message is telling you, and it is so. If
you do a formatted OPEN, you must do a formated READ. If you do an
unformatted OPEN, you must do an unformatted READ. That doesn't actually
have anything to do with the content of the file; you can't do an
unformatted OPEN followed by a formatted READ, no matter what is on the
file. The content of the file would determine whether you should be
doing formatted or unformatted I/O, but a mix lik you have is always
wrong.
Looks to me like most of your problems relate to getting that wrong.
There might also be other problems, but it looks to me like you are
getting whatever real problems there might be mixed up with the problems
caused by not matching the OPEN and the READ. That is causing enough
"smoke" that I can't see through it to what might lie beyond.
Not that "read (30,*)" is a formatted read. I have seen people
incorrectly refer to it as unformatted, which causes no end of
confusion, possibly including yours.
If the file contains text that you can look at and make sense of, then
it is formatted. An unformatted file will look like gibberish if you try
to print or look at it.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Yeah, the file looks like gibberish. And I did tried to use READ(30)
but I still got an error. What should I do?
Strip your code down to a two-line program that opens the file (with
constant file name) and does a read with no input variable list. Remove
all IOSTAT clauses. Your program should use *no* variables.

Post the program and the error message you get when you run it.

You may have said what kind of machine you're running on; please repeat
that information.

Do this to display the first few bytes of your input file, and post the
results:

od -txC input_file | head

The Fortran run-time library (typically? always?) creates files laid out
like this:

<binary length of record 1>
<record 1>
<binary length of record 1>
<binary length of record 2>
<record 2>
<binary length of record 2>

etc.

If your file doesn't look exactly like what's expected, you'll get error
messages if you're lucky and garbage data if you're not.

Louis

The code you asked for is in my previous post ( but with all lines ).

Sometimes it really helps to strip it down to a minimal test case. In this instance, I think we've got enough information.

Here comes the command you mentioned output:

holysword@PRIMAVERA:~$ od -txC /home/rafael/workspace/READTEST/
199408.wam | head

0000000 00 00 00 40 c0 42 80 00 00 00 00 00 c0 39 00 00
0000020 00 00 00 00 42 01 86 1c d9 40 00 00 40 38 00 00
0000040 00 00 00 00 40 39 00 00 00 00 00 00 00 00 00 00
0000060 00 00 00 00 3f a5 63 33 e0 00 00 00 3f f1 99 99
0000100 a0 00 00 00 00 00 00 40 00 00 00 08 40 03 4e 88
0000120 40 00 00 00 00 00 00 08 00 00 00 08 3f bf 7d 83
0000140 e0 00 00 00 00 00 00 08 00 00 00 08 40 41 41 4c
0000160 e0 00 00 00 00 00 00 08 00 00 00 08 3f d2 8d 83
0000200 80 00 00 00 00 00 00 08 00 00 00 08 40 24 48 96
0000220 c0 00 00 00 00 00 00 08 00 00 12 c0 3d fd 22 ef

>
> The machine I'm running now is a Intel Pentium IV HT 3.0, Debian
> Linux, kernel 2.6, Intel Fortran Compile 10.0.023.
> We don't know a lot about the original machine, but it was a 32-bits
> processor with Linux and Intel Fortran Compiler.
>

The good news is that this does look like a Fortran unformatted file. The first record is 64 bytes long (hex 40) and the succeeding records are 8 bytes long. The data look like a mixture of 32-bit integer and 32-bit floating point numbers.

The bad news is that it looks like this file was written on a big-endian machine (Sun SPARC, etc) (i.e., a 32-bit value 0x40 is laid out in memory as 00 00 00 40) and you're running on a little-endian machine (0x40 would be laid out as 40 00 00 00).

I think there's a way to tell your compiler that incoming data needs to be converted from big-endian to little endian. I'll defer to folks who know the details...

For ifort

-convert big_endian

will do the job. However, *everything* will be converted (there may be an additional, run-time, option where you can specify a lun for conversion, ala Lahey, but I don't know).

cheers,

paulv
.