Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Write file delay in Fortran

  1. Feb 22, 2013 #1
    Hi all,
    I encounted a write file problem for Fortran 90. I appreciate it a lot if you can help me.

    I open a file at the beginning of the program. Then everytime I rewind the unit number and write one new line to the first line of the file (the previous data on the first line is replaced).
    So the total size of the file is very small ~1kb. However, I found the written file does not show the data I wrote during the simulation and only show the data when the program is finished. I do not know what is the problem. The following is the stream of the program.

    program test

    open(unit=10, file ='output.dat' ....)

    loop i = 1, 100
    end loop

    end program test

    So during loop, the size of output.dat is always zero and no output until the end of the program. However, I want to check the output during the loop. Does anyone have any idea about this?

    Thanks in advance.
  2. jcsd
  3. Feb 22, 2013 #2


    User Avatar
    Science Advisor
    Gold Member

    When you write to a file from a program, the data gets stored in a buffer in memory and doesn't get copied to disk until some time later, usually when the buffer is full or the program closes the file.

    Look up the FLUSH command. That should solve your problem. (If you have an old version of Fortran with no flush, you can close and reopen the file instead.)
  4. Feb 23, 2013 #3
    Alternatively and given that a program runs way too fast for you to monitor the open file with overwriting write statements, it is best to write without overwriting or simply write to standard output or to standard error.
  5. Feb 25, 2013 #4
    Hi Drgreg,
    Thank you very much for your answer. I setup the opinion buffer="no" in "write", then the problem is solved. Thanks again.

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook