-2

i need a code to remove only a last newline character from a file in TCL. suppose a file

aaa 11
bbb 12
cc 14
newline character

now how to remove that newline character from a file in TCl please help me in this!

4

1 に答える 1

1

シーク切り捨てはあなたの友達です。(Tcl 8.5 以降が必要です。)

set f [open "theFile.txt" r+]

# Skip to where last newline should be; use -2 on Windows (because of CRLF)
chan seek $f -1 end
# Save the offset for later
set offset [chan tell $f]
# Only truncate if we're really sure we've got a final newline
if {[chan read $f] eq "\n"} {
    # Do the truncation!
    chan truncate $f $offset
}

close $f

ファイルの末尾以外の場所からデータを削除するには、ファイルを書き換えるのが最も簡単です (データをすべてメモリにロードするか、ストリーミングして新しいファイルに変換して元に戻すかのいずれかです。後者は難しいですが、大きなファイル)。切り捨ては最後にしか機能しません。

于 2013-08-15T01:01:07.787 に答える