开发者

Tcl variable size limit

I am writing a Tcl script which will be used on an embedded device. The value of a variable in this script will be coming from a text file on the system. My concern is that if the source file is too big this may crash the device as there may not be enough memory to store the entire file. I wonder if the size of the variable can be limited so when feeding the variable it does not exhaust the entire amount of memory.

Also, if possible to limit the size of the variable will it still be filled with as much information as p开发者_StackOverflowossible from the source file, even if the entire file cannot be fed into the variable?


You can limit the size of the variable by specifying the number of characters to read from the file. For example:

set f [open file.dat r]
set var [read $f 1024]

This code will read up to 1024 characters from the file (you'll get less than 1024 characters if the file is shorter than that, naturally).


ISTR, the size limit of a string representation of any variable in v8.5 is limited to 2 GiB. But as Eric already said, in your situation you should not blindly read the file into a variable, but rather either process the contents of the file in chunks or at least first estimate its size using file stat and then read it, if the size is OK (but note that this approach, of course, contains a race condition as the file can grow between the check and the read, but in your case this might or might not be a problem).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜