Re: Read/write binary files in Linux and Windows



Luis Samaniego <luis.samaniego@xxxxxx> wrote:

in Windows: an array x(5,5,2) in real(4) is 800 bytes
in Linux: an array x(5,5,2) real(4) is 200 bytes
[when written to a direct access file with the same recl value]

In my opinion Linux is doing the right thing with respect to the size.
The Windows binary file has trailing zeros.

Does someone know why this happen? What can I do to solve this problem?

First note that it isn't Windows or Linux that is "doing" anything here.
THis is dependent on the compiler - not the OS.

Second, both are right. You are assuming that the units of recl are
bytes. Instead, the units are compiler-dependent. Bytes is the most
common choice of unit, but 4-bytes words is the other. The f2003
standard recommends that bytes be used and most compilers are moving in
that direction. But still, neither answer is "wrong".

Solutions are

1. Most compilers that don't use units of bytes by default have an
option to do so. You could select that option. That does, however,
require you to find the right option for each compiler.

2. The language provides a portable facility to determine the needed
recl value. Use that instead of assuming any particular units of
measure. See the INQUIRE statement - in particular the form with recl=.
Then your code will work as desired for any of the compilers. That's my
recommended solution.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
.



Relevant Pages

  • Re: what is the most suitable Linux platform for Programmers and software developers
    ... C, C++, Perl, Python, Lisp, and a bunch of other languages are supported ... There are FORTRAN compilers, but FORTRAN ... Look at the Linux ...
    (comp.os.linux.misc)
  • Re: g95 and Endianess
    ... Opteron with Linux that reads SGI binary data files ... be very problematic with other compilers, ... end type mytype ...
    (comp.lang.fortran)
  • Re: Coding style article with interesting section on white space
    ... > Linux on Intel x86 at ... > compilers can be much larger, but that has less bearing on portability ... generation on Windows vs Linux (which _should_ pretty obviously be ... and code generator will be essentially unchanged across platforms, ...
    (comp.lang.python)
  • Re: Thank you Microsoft!
    ... Actually, all of the .NET compilers have always been ... > Microsoft is an incredible organization. ... Linux was just cheaper. ... > the speed of your competitivenes. ...
    (microsoft.public.vsnet.general)
  • Re: Mixing C/C++ in the kernel
    ... Linux is written in C, and conspicuously does NOT support C++. ... to run inside the kernel, including templates, exceptions, RTTI, function ... different compilers, I'd say, so this is irrelevant. ... dont kill me, its just how i see things ...
    (comp.os.linux.development.system)