Reputation: 47
While running a tcl script which checks for file existence,the tcl in-built command (file exists
) fails because the filepath and filename (文档名称
) contains chinese characters. I am aware that Unicode characters are supported in Tcl interpreter. I tried command encoding convertfrom
and encoding convertto
but it doesn't resolve the issue.
Also, when I assign Chinese characters to a string in Tcl, say
set a "文档名称"
puts $a
The console prints some weird values ,but not the string with chinese characters. I tried converting the string a to utf-8 using the same encoding command mentioned above, but it didn't work out. I am not sure where I am going wrong in both the cases. Even I tried printing the characters to a file, which also showed some weird characters. Please help me in resolving the issue.
Upvotes: 0
Views: 350
Reputation: 5763
Using the script:
set flist [glob *]
foreach f $flist {
if { [file exists $f] } {
puts "$f: OK"
} else {
puts "$f: NG"
}
}
set a "文档名称"
set f $a
if { [file exists $f] } {
puts "$f: OK"
} else {
puts "$f: NG"
}
Just running tclsh.exe script.tcl
, the last if statement will fail.
The script is read in using the system encoding, and the chinese characters
are not converted correctly. The first loop using the glob
statement works.
Using tclsh.exe -encoding utf-8 script.tcl
, and the final if statement works.
Essentially, the lesson is do not embed utf-8 characters within your script.
Instead, read the data in from a file (using fconfigure $fh encoding utf-8
),
or from another source and then use the data.
The cmd.exe windows console does not support alternate character sets and cannot be used for debugging. It simply will not work. Instead write the debugging output to a file and use notepad to view the file.
Upvotes: 2