title: Writing to file in TCL
body: <p>i have a following code but my report1.out only has last value of k variable. How can I change it so that it writes value of k and then new line and new value.</p>
  
  <p>Will appreciate help.</p>
  
  <h2>Code is:</h2>
  
  <pre><code># Create a procedure
  proc test {} {
      set a 43
      set b 27
      set c [expr $a + $b]
      set e [expr fmod($a, $b)]
      set d [expr [expr $a - $b]* $c]
      puts "c= $c d= $d e=$e"
      for {set k 0} {$k &lt; 10} {incr k} {
          set outfile1 [open "report1.out" w+]        
          puts $outfile1 "k= $k"
          close $outfile1
          if {$k &lt; 5} {
              puts "k= $k k &lt; 5, pow = [expr pow ($d, $k)]"
          } else {
              puts "k= $k k &gt;= 5, mod = [expr $d % $k]"
          }   
      }
  }
  
  # calling the procedure
  test
  </code></pre>