Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Append a file to an existing file

3,472 views
Skip to first unread message

S-Y. Chen

unread,
Aug 3, 2009, 12:12:26 PM8/3/09
to
Hi there

I am wondering if there is a simple command in Tcl that I can append a
file to an existing file, or combine two files into one file ?


Thanks for the help.

Regards
S-Y. Chen

Jeff Godfrey

unread,
Aug 3, 2009, 12:45:39 PM8/3/09
to

Here's one way...

- [open] fileA in append mode
- [open] fileB in read only mode
- [read] the contents of fileB and write it to fileA using [puts]
- [close] both files

So, the man pages on [open], [read], [puts], and [close] should give you
all you need.

Jeff

S-Y. Chen

unread,
Aug 3, 2009, 1:20:43 PM8/3/09
to

I know I can do it through open, read and close.....just want to see
if there is a command like

copy a+b c

Maybe not...so......I will open, read and close it anyway........

Andreas Leitgeb

unread,
Aug 3, 2009, 2:05:05 PM8/3/09
to
S-Y. Chen <shenye...@hotmail.com> wrote:
> On Aug 4, 12:45 am, Jeff Godfrey <jeff_godf...@pobox.com> wrote:
>> S-Y. Chen wrote:
>> > Hi there
>> > I am wondering if there is a simple command in Tcl that I can append a
>> > file to an existing file, or combine two files into one file ?
>> Here's one way...
>> - [open] fileA in append mode
>> - [open] fileB in read only mode

>> - [read] the contents of fileB and write it to fileA using [puts]

just these two steps have a shortcut: [fcopy]

>> - [close] both files

So: open fileA and fileB in their respective modes
fcopy $inputFd $appendtoFd
close both filedescriptors

>> So, the man pages on [open], [read], [puts], and [close] should give you
>> all you need.

same for fcopy, of course.

> copy a+b c
But that's three files: first, copy a to c (using [file copy a c]),
then use the recipe for appending b to c.

PS: if on unix, do it thusly: exec cat a b > c

Georgios Petasis

unread,
Aug 3, 2009, 3:00:37 PM8/3/09
to S-Y. Chen
O/H S-Y. Chen έγραψε:

If you tcllib, you can check the fileutil package:

package require fileutil

::fileutil::appendToFile b [::fileutil::cat a]

or

::fileutil::writeFile c [::fileutil::cat a b]

George

S-Y. Chen

unread,
Aug 4, 2009, 12:30:26 AM8/4/09
to

Thanks a lot !

S-Y. Chen

0 new messages