title: how to update tcl variable value in file using tcl?
body: <p>I have 2 tcl file, in data.tcl file i am keeping flag bits and test.tcl file work based on the flag value . I need to reset the flag value in data.tcl after every test . could you please help me to do the same. I tried the following code but its not working </p>
  
  <p><code>data.tcl</code> file contains flag variable: </p>
  
  <pre><code>set mac 1 
  set xmac 0 
  set fea 0 
  </code></pre>
  
  <p><code>test.tcl</code> script file which has a function to set flag values to 1 or 0 : </p>
  
  <pre><code>  set testcase mac 
    set fp [open "data.tcl" r+] 
  
    while { [gets $fp data] &gt;= 0 } { 
      set var $data 
      if { [lindex $var 1] == $testcase } { 
         set fp1 [open "data.tcl" w+] 
         while { [gets $fp1 data1] &gt;= 0 } { 
              set var1 $data1 
              if { [lindex $var1 1] == $testcase } { 
              set [lindex $var1 2] 0 
              } 
          close $fp1 
       } 
  
    close $fp 
     }   
   } 
  
  close $fp 
  </code></pre>
  
  <p>I have tried the above code but I am not able to update the value of the variable.  Please do help on this. </p>
  
  <p>I am writing automation script, where test runs if the particular flag bit is set to one in data.tcl file. 
  After completing the first task, I need to reset the MAC flag value to 0 and I need to set xmac flag file 1 and remaining flags to 0. </p>
  
  <p>before running test script flag values in data.tcl </p>
  
  <pre><code>set mac 1 
  set xmac 0 
  set fea 0 
  set fea1 0 
  </code></pre>
  
  <p>after 1st run: expected content of data.tcl file: </p>
  
  <pre><code>  set mac 0 
    set xmac 1 
    set fea 0 
    set fea1 0 
  </code></pre>
  
  <p>after 2nd run: expected content of data.tcl file: </p>
  
  <pre><code>  set mac 0 
    set xmac 0 
    set fea 1 
    set fea1 0 
  </code></pre>
  
  <p>after 3rd run: expected content of data.tcl file: </p>
  
  <pre><code>  set mac 0 
    set xmac 0 
    set fea 0 
    set fea1 1 
  </code></pre>
  
  <p>Hope you guys got my requirement. </p>