[comp.lang.pascal] Selective write within files

storm@cs.mcgill.ca (Marc WANDSCHNEIDER) (03/28/91)

Say I have the following file (assigned to TEST.TXT):

-->
This is a nice little file that has a few lines of straight ASCII
text that I wish to manipulate.
After I've written this program, I will then print it out, and give
a copy to my friend <INSERT HERE>
-->


My Question:  Is there any way to just selectively take this text file, 
delete <INSERT HERE> and then rewrite the file to a different name WITHOUT
loading the ENTIRE file into memory and REWRITING the whole thing....?

Ie, I need to search in the text file for somthing (even though I ALWAYs know
where the thing I am looking for is), then remove that word, replace it with
another, and then rewrite the file.

Can I do it, or do I have to load the entire file into memory and then work
with string handlers from there before rewriting.

./*-
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
storm@cs.mcgill.ca         McGill University           It's 11pm, do YOU
Marc Wandschneider         Montreal, CANADA            know what time it is?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

dave@tygra.UUCP (David Conrad) (03/28/91)

In article <1991Mar27.160905.1962@cs.mcgill.ca> storm@cs.mcgill.ca (Marc WANDSCHNEIDER) writes:
>
>My Question:  Is there any way to just selectively take this text file, 
>delete <INSERT HERE> and then rewrite the file to a different name WITHOUT
>loading the ENTIRE file into memory and REWRITING the whole thing....?
>
>Ie, I need to search in the text file for somthing (even though I ALWAYs know
>where the thing I am looking for is), then remove that word, replace it with
>another, and then rewrite the file.
>
>Can I do it, or do I have to load the entire file into memory and then work
>with string handlers from there before rewriting.
>

Why not access it as a File of Char?  Just remember that you then have to
send the CR/LF's and EOF yourself.

Dave Conrad
dave%tygra@sharkey.cc.umich.edu
-- 
=  CAT-TALK Conferencing Network, Computer Conferencing and File Archive  =
-  1-313-343-0800, 300/1200/2400/9600 baud, 8/N/1. New users use 'new'    - 
=  as a login id.  AVAILABLE VIA PC-PURSUIT!!! (City code "MIDET")        =
   E-MAIL Address: dave%tygra@sharkey.cc.umich.edu

derek@sun4dts.dts.ine.philips.nl (derek) (03/28/91)

storm@cs.mcgill.ca (Marc WANDSCHNEIDER) writes: (slightly edited)

[example removed to save bandwidth (whatever that means :-)]

>My Question:  Is there any way to just selectively take [a] text file, 
>delete [a selected text string] and then rewrite the file to a different 
>name WITHOUT
>loading the ENTIRE file into memory and REWRITING the whole thing....?

>Ie, I need to search in the text file for somthing (even though I ALWAYs know
>where the thing I am looking for is), then remove that word, replace it with
>another, and then rewrite the file.

>Can I do it, or do I have to load the entire file into memory and then work
>with string handlers from there before rewriting.

>storm@cs.mcgill.ca         McGill University           It's 11pm, do YOU
>Marc Wandschneider         Montreal, CANADA            know what time it is?

If you _really_ know where it is (e.g. byte offset 325) then just open the
file as a untyped file or file of char (i.e length 1) in read/write mode,
seek to the offset, and write the new text. Of course only if the text
is the same length. You may need to read everything up to the linefeed,
into a buffer, edit the buffer, padding with blanks. (Thus make sure your
<string to be replaced> is _longer_ than anything that will be put there.

Note that if you don't know the exact byte offset in the file, you can 
find it by opening the file first as a text file, reading a line at a time
and counting the characters and lines, until you find the one you are looking
for. Close file, reopen as character, find out what the line terminator is
(LF/CR or LF only perhaps) then add 2*linecount-1 to the character total.
(This step may be unnecessary if you know the lines are CR/LF limited)

I haven't checked this, but am sure it would work. Of course, this would
all be unnecessary if you used a temporary file (called whatever.BAK perhaps)
then read a line, check it, write a line.


Best Regards, Derek Carr
DEREK@DTS.INE.PHILIPS.NL           Philips I&E TQV-5 Eindhoven, The Netherlands 
Standard Disclaimers apply.

CDCKAB%EMUVM1.BITNET@cunyvm.cuny.edu ( Karl Brendel) (03/28/91)

In article <1991Mar27.160905.1962@cs.mcgill.ca>, storm@cs.mcgill.ca
  (Marc WANDSCHNEIDER) wrote:

>Say I have the following file (assigned to TEST.TXT):
>
>-->
>This is a nice little file that has a few lines of straight ASCII
>text that I wish to manipulate.
>After I've written this program, I will then print it out, and give
>a copy to my friend <INSERT HERE>
>-->
>
>My Question:  Is there any way to just selectively take this text
>file, delete <INSERT HERE> and then rewrite the file to a different
>name WITHOUT loading the ENTIRE file into memory and REWRITING the
>whole thing....?
>
>Ie, I need to search in the text file for somthing (even though I
>ALWAYs know where the thing I am looking for is), then remove that
>word, replace it with another, and then rewrite the file.
>
>Can I do it, or do I have to load the entire file into memory and
>then work with string handlers from there before rewriting.

Marc, you really need to specify the Pascal you're using, which
release it is (e.g., Turbo Pascal 5.5), and whether you have the
manuals. (Your other recent postings, re reading Ctrl and Alt chars
and executing an external program, fall clearly into the RTFineM
category.)

_If_ you are using Turbo Pascal (at least for MS-DOS), given that you
know the location and length of the placeholder string
("<INSERT HERE>"), you can simply:

        open in and out files as untyped files of "record size" 1

        BlockRead the in file, BlockWrite the out file to the byte
        immediately before the placeholder

        BlockWrite the outfile with the value to be inserted

        Seek behond the placeholder in the in file

        BlockRead the remainder of the in file and BlockWrite it to
        the out file.

        close the files

Performance will depend largely on the size of the buffer you use
for the BlockReads. If you can use a buffer at least as large as the
larger portion of the in file, you should be able to do this entire
procedure rapidly (relative to the size of the file).

Be sure to RTFineM regarding return values from BlockRead and
BlockWrite.

+--------------------------------------------------------------------+
| Karl Brendel                           Centers for Disease Control |
| Internet: CDCKAB@EMUVM1.BITNET         Epidemiology Program Office |
| Bitnet: CDCKAB@EMUVM1                  Atlanta GA  30093       USA |
|                        Home of Epi Info 5.0                        |
+--------------------------------------------------------------------+