How do I flush output to file after each write with a gfortran Fortran 90 program?

前端 未结 4 1090
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-04 17:31

I am running a loop in a Fortran 90 program that outputs numerical values to an output file for each iteration of the loop. The problem is that the output is not saved to th

4条回答
  •  -上瘾入骨i
    2021-01-04 18:11

    The suggestion from "user152979" was excellent and helpful - 10 years later! I'm using an MS-DOS Fortran 5.1 built prgm to transfer programs and data to a custom-made Z80 SBC (single-board computer). The thing is a little prototype, and has only serial ports. To make it work with an experimental Pentium MMX board, (which runs MS-DOS), I needed a little read-write program. Fortran fit the bill, and the .EXE fits on a diskette (no internet access on the MMX board). But the downloaded data to the Z80 was getting scrambled, if I wrote to the COM1 port.

    Turns out Fortran was buffering the data. I was only getting part of about every 10th record at the Z80. Closing the COM1 file (the output device) and reopening after writing each record of text, caused the buffer to be flushed, and the little Fortran downloader (and the Z80 SBC) now work perfectly.

    So, even if your version of Fortran does not support a "FLUSH" operator, closing and immediately re-opening the file worked fine to flush the buffer contents to the device.

    A Side Note about using DOS to write to COM1 port: I had to strap serial port RS-232c pins CTS to pins DTR, DCD and DSR so that MS-DOS could "see" and write to the serial port. In later versions of MS-DOS (ie. "Windows"), you can use the MODE command to set COM port RTS and CTS values to OFF, but with original DOS, you need to use a soldering iron. AND you need to flush any buffered data, after each record write. User152979 says this close & re-open is "clumsy and slow", but in my case, this trick worked perfectly.

提交回复
热议问题