2
set lambda 1

set lambdaR [open LamdaValue.tr r]
  set Reader [read $lambdaR]
  close $lambdaR

foreach {x} $Reader {
   set lambda $x
  }
set $lambda [expr {$lambda + 1.0}]
set LambdaW [open LamdaValue.tr w]
puts $LambdaW "$lambda"

I'm trying to use this snippet of code to read the value of lambda from a file, modify it, and then write it to the file again. I'm using ns-2 which deals with tcl execution files. But the value of lambda doesn't change... Can you spot where's the error?

1
  • Change set $lambda [expr {$lambda + 1.0}] to set lambda without the $. Commented Mar 23, 2015 at 22:17

1 Answer 1

3

It's best to write a small procedure to read the file and return its contents, and another to write the value back out.

proc readfile {filename} {
    set f [open $filename]
    set data [read $f]
    close $f
    return $data
}
proc writefile {filename data} {
    set f [open $filename w]
    puts -nonewline $f $data
    close $f
}

Then you can simplify the rest of your code a lot:

set lambda 1
# The catch means we use the fallback if the file doesn't exist
catch {set lambda [readfile LamdaValue.tr]}

set lambda [expr {$lambda + 1.0}]

writefile LamdaValue.tr $lambda

The other problem you were having was that you were doing set $lambda. This creates a variable with a strange name. In Tcl, you need to distinguish between reading the variable, when you use $, and naming variable (so a command can update it), when you don't use $. Unless you're wanting to keep the name of one variable in another variable, but it's best to only use that with upvar really to reduce confusion, or to switch to using array elements.

Sign up to request clarification or add additional context in comments.

4 Comments

You are right it worked. Thanks. But I want to ask does that have to do with closing the file after each write and read?
Would you want read -nonewline $f or read $f [file size $filename] ?
@ShaikhaTheGreen It reads the whole file in and writes it out in one go too. It's the simplest thing that might work. You can do other approaches too; this is an area where there a lot of options.
@glennjackman I don't know whether read -nonewline is suitable, but using read with a length is not really necessary now; the buffer management is now efficient enough that it is no big deal to just the single-argument form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.