diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 42c4c5b2cad583dfb395b24dc33ed8a7a036e165..7cb1ad6d59ba4af8c816c25c62c6952998e6e64b 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2004-12-02  Tobias Schlueter  <tobias.schlueter@physik.uni-muenchen.de>
+
+	PR fortran/18170
+	* gfortran.dg/direct_io_3.f90: New test.
+
 2004-12-02  Nathan Sidwell  <nathan@codesourcery.com>
 
 	PR 18758
diff --git a/gcc/testsuite/gfortran.dg/direct_io_3.f90 b/gcc/testsuite/gfortran.dg/direct_io_3.f90
new file mode 100644
index 0000000000000000000000000000000000000000..8603a833e12e1c44d784f5e3f6f45e354b9dcfb2
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/direct_io_3.f90
@@ -0,0 +1,20 @@
+! { dg-do run }
+! PR 18710 : We used to not read and write the imaginary part of 
+! complex numbers
+       COMPLEX C, D
+       DOUBLE COMPLEX E, F
+
+       OPEN(UNIT=9,FILE='PR18710',ACCESS='DIRECT',RECL=132)
+
+       C = (120.0,240.0)
+       WRITE(9,REC=1)C
+       READ(9,REC=1)D
+       if (c /= d) call abort()
+
+       E = (120.0,240.0)
+       WRITE(9,REC=1)E
+       READ(9,REC=1)F
+       if (E /= F) call abort()
+
+       CLOSE(UNIT=9,STATUS='DELETE')
+       END
diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index 2ee79b2b75dc5d3b2c488f637c117abac42fd98e..4dfb3355341124c3af3fc5220f4cfdb5082763b6 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,9 @@
+2004-12-02  Tobias Schlueter  <tobias.schlueter@physik.uni-muenchen.de>
+
+	PR fortran/18710
+	* io/transfer.c (unformatted_read, unformatted_write): width of
+	a COMPLEX is twice its kind.
+
 2004-12-02  Richard Sandiford  <rsandifo@redhat.com>
 
 	* configure.ac: Use TL_AC_GCC_VERSION to set gcc_version.
diff --git a/libgfortran/io/transfer.c b/libgfortran/io/transfer.c
index ceff76fc35c5aa10ad9b3dae4eda13a43d06e83a..c27f0f7bb83d8fc4d7a89e091f0cc48648b612cb 100644
--- a/libgfortran/io/transfer.c
+++ b/libgfortran/io/transfer.c
@@ -271,6 +271,13 @@ unformatted_read (bt type, void *dest, int length)
 {
   void *source;
   int w;
+
+  /* Transfer functions get passed the kind of the entity, so we have
+     to fix this for COMPLEX data which are twice the size of their
+     kind.  */
+  if (type == BT_COMPLEX)
+    length *= 2;
+
   w = length;
   source = read_block (&w);
 
@@ -288,9 +295,14 @@ static void
 unformatted_write (bt type, void *source, int length)
 {
   void *dest;
-   dest = write_block (length);
-   if (dest != NULL)
-     memcpy (dest, source, length);
+
+  /* Correction for kind vs. length as in unformatted_read.  */
+  if (type == BT_COMPLEX)
+    length *= 2;
+
+  dest = write_block (length);
+  if (dest != NULL)
+    memcpy (dest, source, length);
 }