$ cat endfile.f90 integer i open(unit=10,file='test.dat') do i=1,5 write (10,'(I4)') i end do rewind(10) close(10) end $ gfc endfile.f90 && a && cat test.dat 1 2 3 4 $ xxd test.dat 0000000: 2020 2031 0d0a 2020 2032 0d0a 2020 2033 1.. 2.. 3 0000010: 0d0a 2020 2034 0d0a 20 .. 4.. If you remove the rewind statement, the program behaves correctly, that is we have a fifth line with " 5" in it.
Works correctly on i686-pc-linux-gnu.
Well, that was not so difficult: when we write "1234\n6789\n" for example, the length of the write is 10. But the size of the resulting file is 12, since each newline character is translated into a two-byte \n\r sequence! Then, the first step of the rewind is to truncate the file to length 10. I don't know exactly how we should handle this :(
OK, it's worse than I thought. When you open a file with O_BINARY, it is opened using Unix conventions, and you need to add yourself the \r in the right places. Otherwise, you can use the Windows CRLF convention, but then lseek and ftruncate are *not* usable. Not at all.
*** Bug 23264 has been marked as a duplicate of this bug. ***
Patch waiting for some feedback about its interaction with cygwin.
Subject: Bug 23262 CVSROOT: /cvs/gcc Module name: gcc Changes by: fxcoudert@gcc.gnu.org 2005-09-07 21:25:40 Modified files: libgfortran : ChangeLog acinclude.m4 config.h.in configure configure.ac libgfortran/io : transfer.c unix.c Log message: PR libfortran/23262 * acinclude.m4 (LIBGFOR_CHECK_CRLF): New check. * configure.ac: Use new check. * configure.in: Regenerate. * config.h.in: Regenerate. * configure: Regenerate. * io/transfer.c (next_record_w): Add case for CRLF as line terminator. * io/unix.c (tempfile, regular_file): Open files with O_BINARY on systems with CRLF. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/ChangeLog.diff?cvsroot=gcc&r1=1.296&r2=1.297 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/acinclude.m4.diff?cvsroot=gcc&r1=1.6&r2=1.7 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/config.h.in.diff?cvsroot=gcc&r1=1.24&r2=1.25 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/configure.diff?cvsroot=gcc&r1=1.42&r2=1.43 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/configure.ac.diff?cvsroot=gcc&r1=1.32&r2=1.33 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/transfer.c.diff?cvsroot=gcc&r1=1.56&r2=1.57 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/unix.c.diff?cvsroot=gcc&r1=1.36&r2=1.37
Subject: Bug 23262 CVSROOT: /cvs/gcc Module name: gcc Branch: gcc-4_0-branch Changes by: fxcoudert@gcc.gnu.org 2005-09-07 21:31:57 Modified files: libgfortran : ChangeLog acinclude.m4 config.h.in configure configure.ac libgfortran/io : transfer.c unix.c Log message: PR libfortran/23262 * acinclude.m4 (LIBGFOR_CHECK_CRLF): New check. * configure.ac: Use new check. * configure.in: Regenerate. * config.h.in: Regenerate. * configure: Regenerate. * io/transfer.c (next_record_w): Add case for CRLF as line terminator. * io/unix.c (tempfile, regular_file): Open files with O_BINARY on systems with CRLF. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.163.2.88&r2=1.163.2.89 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/acinclude.m4.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.5.12.1&r2=1.5.12.2 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/config.h.in.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.16.12.6&r2=1.16.12.7 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/configure.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.26.12.8&r2=1.26.12.9 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/configure.ac.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.20.12.8&r2=1.20.12.9 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/transfer.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.32.2.15&r2=1.32.2.16 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libgfortran/io/unix.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.21.10.13&r2=1.21.10.14
Patch commited, bug fixed.
*** Bug 23483 has been marked as a duplicate of this bug. ***