From: =?ISO-8859-15?Q?Torsten_Z=FChlsdorff?=
Subject: Beginner Questing: Copying File to another byte by byte
Date:
Message-ID: <fv4ct8$a9v$1@registered.motzarella.org>
Hello,
i want to read a file byte by byte, do some work with each byte and then
write it to another file.
I already figured out how to ready binary files und do some easy work
with the bytes. But i don't know how to write the result in the some
function to another file. Can you help me?
Greetings from Germany,
Torsten
From: Edi Weitz
Subject: Re: Beginner Questing: Copying File to another byte by byte
Date:
Message-ID: <utzhmgqcs.fsf@agharta.de>
On Mon, 28 Apr 2008 13:37:12 +0200, Torsten Z�hlsdorff <···@meisterderspiele.de> wrote:
> i want to read a file byte by byte, do some work with each byte and
> then write it to another file.
>
> I already figured out how to ready binary files und do some easy
> work with the bytes. But i don't know how to write the result in the
> some function to another file. Can you help me?
Use READ-SEQUENCE and WRITE-SEQUENCE. See the function COPY-FILE in
CL-FAD for example code (although the code is probably a tad more
complicated then necessary).
http://weitz.de/cl-fad/
Edi.
--
Lisp is not dead, it just smells funny.
Real email: (replace (subseq ·········@agharta.de" 5) "edi")
Edi Weitz schrieb:
> On Mon, 28 Apr 2008 13:37:12 +0200, Torsten Z�hlsdorff <···@meisterderspiele.de> wrote:
>
>> i want to read a file byte by byte, do some work with each byte and
>> then write it to another file.
>>
>> I already figured out how to ready binary files und do some easy
>> work with the bytes. But i don't know how to write the result in the
>> some function to another file. Can you help me?
>
> Use READ-SEQUENCE and WRITE-SEQUENCE. See the function COPY-FILE in
> CL-FAD for example code (although the code is probably a tad more
> complicated then necessary).
>
> http://weitz.de/cl-fad/
i try to rewrite the copy-file function of cl-fad to:
(defun copy-file (from to)
(with-open-file (in from :element-type '(unsigned-byte 7)
(with-open-file (out to :element-type '(unsigned-byte 7)
:direction :output
:if-exists)
(copy-stream in (make-statistic out))))
(values)))
1. the compiler caught an style-warning about an odd number of
arguments, but i could not figured out, what it means
2. i don't unterstand which function "values" have
The intention of the code is to read a binary file byte by byte and do
some statistic stuff which each byte and return the byte. It is just for
learning. ;)
Greetings,
Torsten
In article <············@registered.motzarella.org>,
Torsten Z�hlsdorff <···@meisterderspiele.de> wrote:
> Edi Weitz schrieb:
> > On Mon, 28 Apr 2008 13:37:12 +0200, Torsten Z�hlsdorff <···@meisterderspiele.de> wrote:
> >
> >> i want to read a file byte by byte, do some work with each byte and
> >> then write it to another file.
> >>
> >> I already figured out how to ready binary files und do some easy
> >> work with the bytes. But i don't know how to write the result in the
> >> some function to another file. Can you help me?
> >
> > Use READ-SEQUENCE and WRITE-SEQUENCE. See the function COPY-FILE in
> > CL-FAD for example code (although the code is probably a tad more
> > complicated then necessary).
> >
> > http://weitz.de/cl-fad/
>
> i try to rewrite the copy-file function of cl-fad to:
>
>
> (defun copy-file (from to)
> (with-open-file (in from :element-type '(unsigned-byte 7)
> (with-open-file (out to :element-type '(unsigned-byte 7)
> :direction :output
> :if-exists)
> (copy-stream in (make-statistic out))))
> (values)))
>
> 1. the compiler caught an style-warning about an odd number of
> arguments, but i could not figured out, what it means
> 2. i don't unterstand which function "values" have
>
> The intention of the code is to read a binary file byte by byte and do
> some statistic stuff which each byte and return the byte. It is just for
> learning. ;)
>
> Greetings,
> Torsten
:if-exists needs an argument
(values) returns nothing, no value. It is sometimes added when you don't want to
return a value from a function.
--
http://lispm.dyndns.org/
From: =?ISO-8859-15?Q?Torsten_Z=FChlsdorff?=
Subject: Re: Beginner Questing: Copying File to another byte by byte
Date:
Message-ID: <fv4h1b$v9a$1@registered.motzarella.org>
Rainer Joswig schrieb:
> In article <············@registered.motzarella.org>,
> Torsten Z�hlsdorff <···@meisterderspiele.de> wrote:
>
>> Edi Weitz schrieb:
>>> On Mon, 28 Apr 2008 13:37:12 +0200, Torsten Z�hlsdorff <···@meisterderspiele.de> wrote:
>>>
>>>> i want to read a file byte by byte, do some work with each byte and
>>>> then write it to another file.
>>>>
>>>> I already figured out how to ready binary files und do some easy
>>>> work with the bytes. But i don't know how to write the result in the
>>>> some function to another file. Can you help me?
>>> Use READ-SEQUENCE and WRITE-SEQUENCE. See the function COPY-FILE in
>>> CL-FAD for example code (although the code is probably a tad more
>>> complicated then necessary).
>>>
>>> http://weitz.de/cl-fad/
>> i try to rewrite the copy-file function of cl-fad to:
>>
>>
>> (defun copy-file (from to)
>> (with-open-file (in from :element-type '(unsigned-byte 7)
>> (with-open-file (out to :element-type '(unsigned-byte 7)
>> :direction :output
>> :if-exists)
>> (copy-stream in (make-statistic out))))
>> (values)))
>>
>> 1. the compiler caught an style-warning about an odd number of
>> arguments, but i could not figured out, what it means
>> 2. i don't unterstand which function "values" have
>>
>> The intention of the code is to read a binary file byte by byte and do
>> some statistic stuff which each byte and return the byte. It is just for
>> learning. ;)
>>
>> Greetings,
>> Torsten
>
> :if-exists needs an argument
i changed it to:
(defun copy-file (from to)
(with-open-file (in from :element-type '(unsigned-byte 7)
(with-open-file (out to :element-type '(unsigned-byte 7)
:direction :output
:if-exists :supersede)
(copy-stream in (make-statistic out))))
(values)))
but know the variable "in" is unbound? i don't understand why.
> (values) returns nothing, no value. It is sometimes added when you don't want to
> return a value from a function.
i understand. :)
Greetings,
Torsten
In article <············@registered.motzarella.org>,
Torsten Z�hlsdorff <···@meisterderspiele.de> wrote:
> Rainer Joswig schrieb:
> > In article <············@registered.motzarella.org>,
> > Torsten Z�hlsdorff <···@meisterderspiele.de> wrote:
> >
> >> Edi Weitz schrieb:
> >>> On Mon, 28 Apr 2008 13:37:12 +0200, Torsten Z�hlsdorff <···@meisterderspiele.de> wrote:
> >>>
> >>>> i want to read a file byte by byte, do some work with each byte and
> >>>> then write it to another file.
> >>>>
> >>>> I already figured out how to ready binary files und do some easy
> >>>> work with the bytes. But i don't know how to write the result in the
> >>>> some function to another file. Can you help me?
> >>> Use READ-SEQUENCE and WRITE-SEQUENCE. See the function COPY-FILE in
> >>> CL-FAD for example code (although the code is probably a tad more
> >>> complicated then necessary).
> >>>
> >>> http://weitz.de/cl-fad/
> >> i try to rewrite the copy-file function of cl-fad to:
> >>
> >>
> >> (defun copy-file (from to)
> >> (with-open-file (in from :element-type '(unsigned-byte 7)
> >> (with-open-file (out to :element-type '(unsigned-byte 7)
> >> :direction :output
> >> :if-exists)
> >> (copy-stream in (make-statistic out))))
> >> (values)))
> >>
> >> 1. the compiler caught an style-warning about an odd number of
> >> arguments, but i could not figured out, what it means
> >> 2. i don't unterstand which function "values" have
> >>
> >> The intention of the code is to read a binary file byte by byte and do
> >> some statistic stuff which each byte and return the byte. It is just for
> >> learning. ;)
> >>
> >> Greetings,
> >> Torsten
> >
> > :if-exists needs an argument
>
> i changed it to:
>
> (defun copy-file (from to)
> (with-open-file (in from :element-type '(unsigned-byte 7)
> (with-open-file (out to :element-type '(unsigned-byte 7)
> :direction :output
> :if-exists :supersede)
> (copy-stream in (make-statistic out))))
> (values)))
>
> but know the variable "in" is unbound? i don't understand why.
You need to close the first list of the first WITH-OPEN-FILE.
The typical pattern for these macros is:
(with-something ( fixed-arg0 .. fixed-argn :key0 keyarg0 ... keyn keyargn )
body-expression1
..
body-expressionn)
Use semi-automatic indenting. Select the whole form and let the editor reindent it.
Indentation will show many of the simple problems. Color-coding also gives visual
clues...
Indenting your code gives me:
(defun copy-file (from to)
(with-open-file (in from :element-type '(unsigned-byte 7)
(with-open-file (out to :element-type '(unsigned-byte 7)
:direction :output
:if-exists :supersede)
(copy-stream in (make-statistic out))))
(values)))
You can easily see that there is a problem.
>
> > (values) returns nothing, no value. It is sometimes added when you don't want to
> > return a value from a function.
>
> i understand. :)
>
> Greetings,
> Torsten
--
http://lispm.dyndns.org/
From: Edi Weitz
Subject: Re: Beginner Questing: Copying File to another byte by byte
Date:
Message-ID: <ulk2ygo5g.fsf@agharta.de>
On Mon, 28 Apr 2008 14:18:13 +0200, Torsten Z�hlsdorff <···@meisterderspiele.de> wrote:
> (with-open-file (in from :element-type '(unsigned-byte 7)
> (with-open-file (out to :element-type '(unsigned-byte 7)
I'd be surprised if your Lisp implementation really had support for
file I/O in 7-bit chunks. For simply copying a file, octets -
i.e. (UNSIGNED-BYTE 8) - should do. If you really need strange byte
sizes like above, have a look at this:
http://weitz.de/odd-streams/
Edi.
--
Lisp is not dead, it just smells funny.
Real email: (replace (subseq ·········@agharta.de" 5) "edi")
Edi Weitz schrieb:
>> (with-open-file (in from :element-type '(unsigned-byte 7)
>> (with-open-file (out to :element-type '(unsigned-byte 7)
>
> I'd be surprised if your Lisp implementation really had support for
> file I/O in 7-bit chunks. For simply copying a file, octets -
> i.e. (UNSIGNED-BYTE 8) - should do. If you really need strange byte
> sizes like above, have a look at this:
>
> http://weitz.de/odd-streams/
I am using SBCL on an FreeBSD. Is there no warning if the element-type
is "upgraded" to 8 Byte? I really do need this byte size.
Greetings,
Torsten
From: Edi Weitz
Subject: Re: Beginner Questing: Copying File to another byte by byte
Date:
Message-ID: <u8wyygmdh.fsf@agharta.de>
On Mon, 28 Apr 2008 14:51:54 +0200, Torsten Z�hlsdorff <···@meisterderspiele.de> wrote:
> I am using SBCL on an FreeBSD. Is there no warning if the
> element-type is "upgraded" to 8 Byte?
I don't think so. This is on Linux:
···@miles:~$ sbcl
This is SBCL 1.0.5, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.
SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses. See the CREDITS and COPYING files in the
distribution for more information.
* (defun foo (n) (with-open-file (in "/etc/passwd" :element-type `(unsigned-byte ,n)) (loop repeat 10 collect (read-byte in))))
FOO
* (foo 7)
(114 111 111 116 58 120 58 48 58 48)
* (foo 8)
(114 111 111 116 58 120 58 48 58 48)
* (foo 9)
(28530 29807 30778 12346 12346 29242 28527 14964 29231 28527)
* (foo 16)
(28530 29807 30778 12346 12346 29242 28527 14964 29231 28527)
> I really do need this byte size.
See the link in my previous posting.
Edi.
--
Lisp is not dead, it just smells funny.
Real email: (replace (subseq ·········@agharta.de" 5) "edi")
Edi Weitz <········@agharta.de> writes:
> file I/O in 7-bit chunks. For simply copying a file, octets -
> i.e. (UNSIGNED-BYTE 8) - should do. If you really need strange byte
Or you could use stream-element-type like this:
(defun copy-file (inpathname outpathname &optional (buffersize #.(* 8 1024)))
"my standard buffered file copy routine"
(with-open-file (is inpathname :direction :input)
(with-open-file (os outpathname :direction :output
:if-exists :supersede
:if-does-not-exist :create)
(let ((buffer (make-array buffersize :element-type (stream-element-type is))))
(do ((nread (read-sequence buffer is) (read-sequence buffer is))
(total 0 (+ total nread)))
((zerop nread) total)
(write-sequence buffer os :end nread)))))))
Petter
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Richard M Kreuter
Subject: Re: Beginner Questing: Copying File to another byte by byte
Date:
Message-ID: <87skx6kmw0.fsf@progn.net>
Petter Gustad <·············@gustad.com> writes:
> Edi Weitz <········@agharta.de> writes:
>
>> file I/O in 7-bit chunks. For simply copying a file, octets -
>> i.e. (UNSIGNED-BYTE 8) - should do. If you really need strange byte
>
> Or you could use stream-element-type like this:
>
>
> (defun copy-file (inpathname outpathname &optional (buffersize #.(* 8 1024)))
> "my standard buffered file copy routine"
> (with-open-file (is inpathname :direction :input)
> (with-open-file (os outpathname :direction :output
> :if-exists :supersede
> :if-does-not-exist :create)
> (let ((buffer (make-array buffersize :element-type (stream-element-type is))))
> (do ((nread (read-sequence buffer is) (read-sequence buffer is))
> (total 0 (+ total nread)))
> ((zerop nread) total)
> (write-sequence buffer os :end nread)))))))
Because you don't specify the element-type of the input or output
streams, they'll default to CHARACTER, and so the routine stands a
decent chance of (a) losing in case the input file isn't validly
encoded character data for the default external-format, (b) being
slower than necessary, owing to two rounds of character transcoding.
(Additionally, the file copying routines posted so far don't preserve
any file metadata, and don't even try to do something reasonable about
sparse files. So ISTM that these kinds of commonly offered
copy-file/copy-stream functions should be used with caution, at least
when the CL program might interact with an external system that could
care about such details.)
--
RmK
Richard M Kreuter <·······@progn.net> writes:
> Petter Gustad <·············@gustad.com> writes:
>
>> Edi Weitz <········@agharta.de> writes:
>>
>>> file I/O in 7-bit chunks. For simply copying a file, octets -
>>> i.e. (UNSIGNED-BYTE 8) - should do. If you really need strange byte
>>
>> Or you could use stream-element-type like this:
>>
>>
>> (defun copy-file (inpathname outpathname &optional (buffersize #.(* 8 1024)))
>> "my standard buffered file copy routine"
>> (with-open-file (is inpathname :direction :input)
>> (with-open-file (os outpathname :direction :output
>> :if-exists :supersede
>> :if-does-not-exist :create)
>> (let ((buffer (make-array buffersize :element-type (stream-element-type is))))
>> (do ((nread (read-sequence buffer is) (read-sequence buffer is))
>> (total 0 (+ total nread)))
>> ((zerop nread) total)
>> (write-sequence buffer os :end nread)))))))
>
> Because you don't specify the element-type of the input or output
> streams, they'll default to CHARACTER, and so the routine stands a
> decent chance of (a) losing in case the input file isn't validly
> encoded character data for the default external-format, (b) being
> slower than necessary, owing to two rounds of character transcoding.
>
> (Additionally, the file copying routines posted so far don't preserve
> any file metadata, and don't even try to do something reasonable about
> sparse files. So ISTM that these kinds of commonly offered
> copy-file/copy-stream functions should be used with caution, at least
> when the CL program might interact with an external system that could
> care about such details.)
But lisp file and stream routines are not meant to interact with an
external system. They're only meant as an entry point to permanent
storage FROM the lisp image point of view.
Ideally, only data written from lisp should be read from lisp.
Implement your editor in lisp, and read only data files edited with
your lisp editor.
You may convert a native file to a lisp file, using FFI to get access
to the native file system routines, where you can read all the
attributes and forks and whatever you want, and convert them if you
will to lisp files.
--
__Pascal Bourguignon__
Richard M Kreuter schrieb:
> Petter Gustad <·············@gustad.com> writes:
>
>> Edi Weitz <········@agharta.de> writes:
>>
>>> file I/O in 7-bit chunks. For simply copying a file, octets -
>>> i.e. (UNSIGNED-BYTE 8) - should do. If you really need strange byte
>> Or you could use stream-element-type like this:
>>
>>
>> (defun copy-file (inpathname outpathname &optional (buffersize #.(* 8 1024)))
>> "my standard buffered file copy routine"
>> (with-open-file (is inpathname :direction :input)
>> (with-open-file (os outpathname :direction :output
>> :if-exists :supersede
>> :if-does-not-exist :create)
>> (let ((buffer (make-array buffersize :element-type (stream-element-type is))))
>> (do ((nread (read-sequence buffer is) (read-sequence buffer is))
>> (total 0 (+ total nread)))
>> ((zerop nread) total)
>> (write-sequence buffer os :end nread)))))))
>
> Because you don't specify the element-type of the input or output
> streams, they'll default to CHARACTER, and so the routine stands a
> decent chance of (a) losing in case the input file isn't validly
> encoded character data for the default external-format, (b) being
> slower than necessary, owing to two rounds of character transcoding.
>
> (Additionally, the file copying routines posted so far don't preserve
> any file metadata, and don't even try to do something reasonable about
> sparse files. [..]
This is my intention. I do not care about metadata, i just care about
the bytes. I want some statistic about the bytes of the given file and
try to create a 1:1 copy.
Greetings,
Torsten
Torsten Z�hlsdorff <···@meisterderspiele.de> writes:
> Hello,
>
> i want to read a file byte by byte, do some work with each byte and
> then write it to another file.
>
> I already figured out how to ready binary files und do some easy work
> with the bytes. But i don't know how to write the result in the some
> function to another file. Can you help me?
How did you do for the input file?
It's exactly the same for the output file, just change the :direction
:input into a :direction :output, and use write-byte instead of
read-byte (write-sequence instead of read-sequence).
clhs with-open-file
clhs open
--
__Pascal Bourguignon__