Fix PR libfortran/39667
[gcc.git] / libgfortran / io / intrinsics.c
index 0a894aac43ecf4e2e95abf90e864a1492a86fe47..0e33e8490daa92f85e3bef84ab0a6b55a5afa18e 100644 (file)
@@ -46,6 +46,13 @@ PREFIX(fgetc) (const int * unit, char * c, gfc_charlen_type c_len)
   if (u == NULL)
     return -1;
 
+  fbuf_reset (u);
+  if (u->mode == WRITING)
+    {
+      sflush (u->s);
+      u->mode = READING;
+    }
+
   memset (c, ' ', c_len);
   ret = sread (u->s, c, 1);
   unlock_unit (u);
@@ -118,6 +125,13 @@ PREFIX(fputc) (const int * unit, char * c,
   if (u == NULL)
     return -1;
 
+  fbuf_reset (u);
+  if (u->mode == READING)
+    {
+      sflush (u->s);
+      u->mode = WRITING;
+    }
+
   s = swrite (u->s, c, 1);
   unlock_unit (u);
   if (s < 0)