# How to save solution in a loop?

**URL:** <https://community.freefem.org/t/how-to-save-solution-in-a-loop/1576>\
**Category:** General Discussion\
**Created:** [March 5, 2022, 6:34pm UTC](https://community.freefem.org/t/how-to-save-solution-in-a-loop/1576 "2022-03-05T18:34:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lietvo](https://avatars.discourse-cdn.com/v4/letter/l/e99b99/32.png) [@lietvo](https://community.freefem.org/u/lietvo)\
**Post date:** [March 5, 2022, 6:34pm UTC](https://community.freefem.org/t/how-to-save-solution-in-a-loop/1576/1 "2022-03-05T18:34:05Z")

</div>

Hello everyone,  
I want to save solutions at each time iteration of heat/Stokes equation. I know how to save solution for the Poisson equation with function ‘ofstream’ but I don’t know how to storage the solutions of the heat equation in a “for” loop of time iterations. I really appreciate any idea or answer!  
Thank you very much!

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://community.freefem.org/u/Robert)\
**Post date:** [March 5, 2022, 7:01pm UTC](https://community.freefem.org/t/how-to-save-solution-in-a-loop/1576/2 "2022-03-05T19:01:30Z")

</div>

Hello lietvo,

You could try the following:

```auto
int iter=.... // number of iterations
int i;
for(int i = 0; i<=iter; i++)
{
...
   {
    ofstream file("Heat.txt",append);
    file<<Heat<<endl; // or Heat(x,y) if you have a desired monitoring point
   }
}

```

“append” should save the solution at each iteration using the same file.

Or, If you need separated files

```auto
int iter=.... // number of iterations
int i;
for(int i = 0; i<=iter; i++)
{
...
   {
    ofstream file("Heat"+i+".txt");
    file<<Heat<<endl; 
   }
}

```

Additionally you could add “if” conditions before ofstream to select a range of iterations.

Hope it helps,  
Robert

---

<div class="post-metadata">

**Author:** ![lietvo](https://avatars.discourse-cdn.com/v4/letter/l/e99b99/32.png) [@lietvo](https://community.freefem.org/u/lietvo)\
**Post date:** [March 5, 2022, 7:21pm UTC](https://community.freefem.org/t/how-to-save-solution-in-a-loop/1576/3 "2022-03-05T19:21:18Z")

</div>

Hello Robert,  
Thank you very much! It works well! I am very grateful to see your answer!
