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

In summary: However, if you set the "opinion buffer" to "no", the data will be written directly to disk without going through the memory buffer. Thanks for the clarification!
  • #1
xxh418
11
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
  • #2
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.)
 
  • #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.
 
  • #4
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
 
  • #5


There could be a few reasons why you are experiencing this issue. One possibility is that you are not properly closing the file after each write operation, causing the data to not be written to the file until the program is finished. Another possibility is that you are not using the correct file unit number when writing to the file, which could result in the data being written to a different file or not being written at all.

To write file delay in Fortran, you can try using the FLUSH statement after each write operation to ensure that the data is immediately written to the file. Additionally, you can also try using the INQUIRE statement to check the status of the file before and after each write operation to make sure that the data is being written correctly.

Overall, it is important to make sure that you are properly managing the file and using the correct file unit number when writing to it. I hope this helps and good luck with your program!
 

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

1. What is "write file delay" in Fortran?

Write file delay in Fortran refers to the amount of time it takes for a program to write data to a file. This is important in order to ensure that the data is accurately and efficiently written to the file.

2. Why is write file delay important in Fortran?

Write file delay is important in Fortran because it directly affects the performance of a program. If the delay is too long, it can slow down the execution of the program and affect its overall efficiency.

3. How can I reduce write file delay in Fortran?

There are a few ways to reduce write file delay in Fortran. One way is to use the "flush" command after each write statement, which forces the program to write the data immediately to the file. Another way is to use the "buffered" option when opening the file, which allows the program to write data to the file in larger chunks rather than after each individual write statement.

4. What factors can affect write file delay in Fortran?

There are several factors that can affect write file delay in Fortran. These include the size of the file, the speed of the hard drive, the number of write statements in the program, and the use of the "flush" or "buffered" options.

5. How can I measure write file delay in Fortran?

Write file delay can be measured by using the "system_clock" function in Fortran. This function returns the number of clock ticks since the program started, which can be used to calculate the amount of time it takes for a write statement to execute. This can be repeated multiple times to get an average write file delay.

Similar threads

  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
745
  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
2
Replies
41
Views
4K
  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top