Why is My Fortran Program Only Showing Output Data After Completion?

  • Context: Fortran 
  • Thread starter Thread starter xxh418
  • Start date Start date
  • Tags Tags
    Delay File Fortran
Click For Summary

Discussion Overview

The discussion revolves around a Fortran 90 programming issue related to file output, specifically why data written to a file during a loop is not visible until the program completes. Participants explore potential solutions and workarounds for monitoring output in real-time during program execution.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a problem where data written to a file during a loop does not appear until the program finishes, leading to confusion about the output process.
  • Another participant explains that data is buffered in memory and may not be written to disk until the buffer is full or the file is closed, suggesting the use of the FLUSH command to address this issue.
  • A different approach is proposed, recommending that instead of overwriting the file, the participant could write without overwriting or use standard output or standard error for monitoring purposes.
  • A later reply indicates that setting the buffer option to "no" resolved the issue for the original poster, confirming that the problem was related to buffering behavior.

Areas of Agreement / Disagreement

Participants generally agree on the buffering behavior of file writing in Fortran and suggest methods to address the issue. However, there are multiple approaches proposed, including using FLUSH, avoiding overwriting, and changing buffer settings, indicating that no single solution is universally accepted.

Contextual Notes

Participants mention the limitations of older Fortran versions regarding the FLUSH command and the implications of using different buffering strategies. The discussion highlights the dependency on specific programming practices and settings.

xxh418
Messages
9
Reaction score
0
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
rewind(10)
write(10,*)data
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.
 
Technology news on Phys.org
xxh418 said:
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
rewind(10)
write(10,*)data
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.
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.)
 
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.
 
DrGreg said:
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.)

Hi Drgreg,
Thank you very much for your answer. I setup the opinion buffer="no" in "write", then the problem is solved. Thanks again.

Xu
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K