Search code examples
tcl

cut off beginning of file without "read"ing the rest


I have a script that that writes a small amount of encrypted information to a file, and then appends another file using cat:

exec cat $in.header $in.db > $in.dbx

The $in.header file is always the same size. Is there a way to extract $in.db from $in.dbx without "read"ing the rest of the file? This is what it looks like now:

set outfile [open $out.db wb]
while {![eof $infile]} {
    set input [read $infile 4096]
    puts -nonewline $outfile $input
}

It works, but I have to think that there's a better way, some of the files can be quite large.


Solution

  • You can't chop off the beginning of a file without copying it; no modern operating system offers you the ability to do that. You can sometimes tell code to start looking in a file from a particular position onwards, but only sometimes.

    You can use seek (aka chan seek) to move around in an open file, and if you are copying data out then consider using fcopy; it's extremely fast, especially when doing a binary-to-binary copy.