[futurebasic] Writing to a text file

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2003 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Fri, 11 Jul 2003 02:20:44 -0400
This one appears so simple that I am almost embarrassed to ask, but here goes:

Assume I have a large amount (greater than what can be held in a edit 
field) of ASCII text such as the following first few lines (of many 
hundreds or, perhaps, thousands or lines):

// We're working with a lot of data, so crank up the memory
// Hey, I gotta gig or RAM, whatcha complainin' about!?!??

dim system 50000000

dim as container gC
gC  = "Twas brillig, and the slithy toves"
gC += "Did gyre and gimble in the wabe:"
gC += "All mimsy were the borogoves,"
gC += "And the mome raths outgrabe."
gC +=  ...ad infinitum (or at least a few thousand more lines.)

Once I have the text in the container, I perform some parsing on it 
then, with Alain's fn Split for containers from the Rosetta Stone 
library...:

begin globals
dynamic splitarray( 50000 ) as str255
dim gc                  as container
end globals

local fn Split( @cptr as ptr, splitchar as str15 )
dim as pointer pstart, pend, eoh
dim as integer splits, i, delim, l

if cptr.nil& = _nil then exit fn
l      = fn gethandlesize(cptr.nil&)'cptr.nil&[0]
long if l > 0
pstart = [cptr.nil&]
pend   = pstart
eoh    = pstart + l
delim  = splitchar[1]
splits = 0

| @splitarray(splits), 0

for i = 0 to l - 1
long if |pend| != delim
long if pend - pstart < 255
pend++
xelse
| @splitarray(splits), 255
blockmove(pstart,@splitarray(splits) + 1, pend - pstart)
pstart = pend
splits++
do
pend++
until |pend| = delim or pend = eoh
pstart = pend
end if
xelse
| @splitarray(splits), pend - pstart
blockmove(pstart,@splitarray(splits) + 1, pend - pstart)
pend++
pstart = pend
splits++
end if

next
long if (pend - pstart)
poke @splitarray(splits), pend - pstart
blockmove(pstart,@splitarray(splits) + 1, pend - pstart)
end if

compress dynamic splitarray
end if
end fn = splits


...I use a space ( chr$(32) ) as a delimiter and split the massaged 
container text into a dynamic array of words and/or words and 
punctuation:

dim as long  lastElement
dim as str15 delimStr

delimStr = chr$(32)

lastElement = fn Split( gC, delimStr )

Once I have the array, I do a Quick Sort and do some other work on 
individual elements in it, basically creating a dictionary of sorts.

Here is my problem:

How do I best write the final array of words and/or words with 
punctuation to a text file that can be opened and read with a word 
processor.

Do I directly write the contents of the array to a handle:

dim as long i

route _toBuffer + 2

for i = 0 to lastElement
print splitarray( i )
next i

route _toScreen

and then write the resulting gFBbuffer(2) handle to a file (which 
doesn't seem to work)?

Or do I empty the container and dump the array contents back into it...

dim as long i

gC = ""

for i = 0 to lastElement
gC += splitarray( i )
next i

... then kill the array for memory's sake...:

kill dynamic splitarray

... and then work with the container handle:

dim as handle myContainerHandle

myContainerHandle = [@gC]

Open file
Check Size
Create Handle of that Size
Lock Handle
Read File Data into Handle
Unlock Handle
Close File
Create New file
Open New file
Lock Handle
Write data to disk file
Unlock handle
Close file
Dispose Handle

Or am I totally missing the picture (the most likely scenario)?

Remember, the file I am creating is not binary data to be read back 
into the program at a future time, but rather a text file suitable 
for opening with a word processor such as BBEdit. And it is very 
large (easily demonstrating why FB^3 needs edit fields that can hold 
more than 32K.)

Any suggestions are appreciated.

Ken