Skip to content
Advertisement

Save all data into variable tcl

I have a code which i want to use to open a log file, extract the contents and save it into a variable so i can extract the data from the variable in the future. How do i do that? So far the code saves only the last data of the log file into the variable. I think the while loop grabs data line by line and the variable stores it one by one and overwrites the previous data as it progresses, how do i change the code so that it stores all the data as the code progresses?

set fp [open "filename.log" r]
while { [gets $fp data] >= 0 } { 
    set written $data
}
close $fp 
puts $written

please help

Advertisement

Answer

You can make this with append command:

set written ""
set fp [open "filename.log" r]
while { [gets $fp data] >= 0 } { 
    # append last data read
    append written $data
    # add some line feed for readability (may not be necessary)
    append written "n"
}
close $fp 
puts $written
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement