# How to force writing output files in real-time?

**URL:** https://community.freefem.org/t/how-to-force-writing-output-files-in-real-time/1435
**Category:** General Discussion
**Created:** [January 15, 2022, 6:07pm UTC](https://community.freefem.org/t/how-to-force-writing-output-files-in-real-time/1435 "2022-01-15T18:07:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![david.fabre](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@david.fabre](https://community.freefem.org/u/david.fabre)
#### Post date: [January 15, 2022, 6:07pm UTC](https://community.freefem.org/t/how-to-force-writing-output-files-in-real-time/1435/1 "2022-01-15T18:07:55Z")

</div>

Hello all !

One simple question. I use communication files to check the status of freefem programs from Matlab. For this I want to write in such communication files in real-time. I can see that it is usually not the case ; I assume that freefem stores the output in some buffer and writes only when there is a sufficient ammount of things to be written. This leads to some delay, especially in nohup mode.

So is there a way to force the writing (and closing) of files before continuing the program ?

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [January 15, 2022, 7:17pm UTC](https://community.freefem.org/t/how-to-force-writing-output-files-in-real-time/1435/2 "2022-01-15T19:17:04Z")

</div>

If you scope the variable definition, it will be written at the end of its lifetime.

---

<div class="post-metadata">

### Author: ![fivanci](https://avatars.discourse-cdn.com/v4/letter/f/e95f7d/32.png) [@fivanci](https://community.freefem.org/u/fivanci)
#### Post date: [January 16, 2022, 7:00am UTC](https://community.freefem.org/t/how-to-force-writing-output-files-in-real-time/1435/3 "2022-01-16T07:00:57Z")

</div>

Not quite sure if I understood the question well, but how about `flush` it ? E.g., I often do something like this when I want to check the progress before simulation ends:

```auto
// open file in append mode
ofstream file("filename.txt",append); 
...
for(...){
      ... some calculations ...
      file << ... << endl; // write in the file
      if(...) file.flush; // e.g. flush every five time steps
}

```

Seems to be working well for me.

if

---

<div class="post-metadata">

### Author: ![david.fabre](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@david.fabre](https://community.freefem.org/u/david.fabre)
#### Post date: [January 16, 2022, 9:49pm UTC](https://community.freefem.org/t/how-to-force-writing-output-files-in-real-time/1435/4 "2022-01-16T21:49:01Z")

</div>

Thanks ! flush seems to be the precise thing I need !  
Cheers  
D
