[futurebasic] Re: [FB] files question

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 1998 : Group Archive : Group : All Groups

From: Rick Brown <rbrown@...>
Date: Sun, 22 Nov 1998 10:28:02 -0600
Warren wrote:
> I've got a rather lengthy text file that has to be edited at about one
> hundred places by the user.  I've set up an interface that gets all the
> info necessary to make those edits, but can't find a way to insert them.

Let's start with the simplest possibility.  In the simplest case, you
would be replacing certain strings in the original file with certain
other strings of _identical_ lengths to the original ones.  You haven't
said whether all the editing falls into that category; but if it does,
you're in luck.  To replace a string in this case, all you need do is:

1) When you open the file, specify a recLen of 1.
2) Note the position in the file where the (old) string
   starts (you can do this with the REC function);
3) When you get the new string from the user, set the
   file mark to the position you noted in Step 1 (you
   can use the RECORD statement to do this.
4) print the string to the file.  The simplest way is
   probably to use the PRINT# statement, and you will
   probably want to put a semicolon at the end of the
   statement to inhibit the writing of a Carriage-return
   character after the string.

In case your file consists of fixed-length records, it's almost as
simple.  In this case, you'd (1) specify the appropriate recLen when
opening the file; (2) use both the REC and POS functions to note the
position of the old string; and (3) use both the "record" and
"positionInRecord" parameters in the RECORD statement, when setting the
file mark before writing the new string.  Step (4) would be the same as
above.

Now let's take a slightly more complicated example.  Say the above
techniques don't fit your needs, but the file happens to be small enough
that you can fit it all into memory at once.  In that case, I would do
the following:

1) Create a new relocatable block (a "handle"-type block),
   and read the entire file into the block.  You can use
   READ FILE to do this.
2) Use FN MUNGER to add and/or delete and/or replace strings.
   MUNGER will automatically adjust the size of the block as
   necessary.
3) Write the updated block back to disk.  You can use WRITE FILE
   to do this.

Now let's suppose the worst case: that you can't assume fixed-length
strings/records, and that your file is too big to fit into memory.  Then
the only solution I see is something like this:

1) Create a temporary output file.  Let's say it's called "temp".
2) Do the following loop until the entire file is written:
   a) Read a chunk of the original file, up to the point
      where an edited string needs to go;
   b) Write that chunk out to "temp".
   c) Write the edited string (if any) out to "temp"
   d) Skip over the original string in the original file.
   e) Go to (a)
3) Delete the original file.
4) Rename "temp" to the original file's name.

Hope this gives you some ideas.

- Rick