![]() |
||
| Home | SwiftForth Archive | SwiftX Archive | |

FILE>MEM treats all files the same: the entire file is read into
allocated memory. Do with it what you want, just remember to FREE the
ALLOCATEd memory block.
: TEST ( -- ) \ Display file contents
S" myfile.bin" FILE>MEM ( MemAddress FileSize )
OVER >R ( save MemAddress to FREE later )
\ Next, do whatever is needed with file contents
( MemAddress FileSize ) DUMP ( Show File Contents )
R> ( MemAddress ) FREE THROW ( Release Memory - we must do this! )
;
Mike
-----Original Message-----
From: sftalk-bounce_at_forth.com [mailto:sftalk-bounce_at_forth.com]On
Behalf
Of Damir
Sent: Saturday, January 12, 2002 11:04 AM
To: sftalk_at_forth.com
Subject: [sftalk] Re: File to memory and memory to file allot + mem to
swiftforth
On Mon, 3 Dec 2001 10:51:02 -0800, you wrote:
>
>An example of file access and memory allocation:
>
>\ Use S" filename.ext" FILE>MEM
>: FILE>MEM ( filename count -- memAddr filesize )
> R/O OPEN-FILE THROW >R
> R@ FILE-SIZE THROW D>S ( size )
> DUP ALLOCATE THROW ( Allocate a Memory Block )=20
> DUP ROT R@ READ-FILE THROW ( bytes-read )
> R> CLOSE-FILE THROW ;
>
>: TEST ( -- ) \ Display file contents
> S" myfile.txt" FILE>MEM ( MemAddress FileSize )=20
> CR 2DUP TYPES ( Display File Contents )=20
> DROP ( discard size )=20
> FREE THROW ( Release Memory - we must do this! ) ;
>
>
>Note that TYPES is a version of TYPE that handles line feeds etc.
>
>Mike
What to do if extension is bin, S" myfile.bin"?
Thanks.
=20
Damir
----------------------------------------------------------------------
sftalk_at_forth.com The SwiftForth programming discussion email list
To unsubscribe, send subject "unsubscribe sftalk" to listar_at_forth.com
For help with listar commands, send subject "help" to listar_at_forth.com
Archives are located at http://www.forth.com/sftalk -- check them out!
Search the archives! Visit http://www.forth.com/search for details.
----------------------------------------------------------------------
sftalk_at_forth.com The SwiftForth programming discussion email list
To unsubscribe, send subject "unsubscribe sftalk" to listar_at_forth.com
For help with listar commands, send subject "help" to listar_at_forth.com
Archives are located at http://www.forth.com/sftalk -- check them out!
Search the archives! Visit http://www.forth.com/search for details.
Received on Sat Jan 12 2002 - 11:16:34 PST
This archive was generated by hypermail 2.2.0 : Fri Nov 21 2008 - 03:04:21 PST