Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!hsdndev!cmcl2!adm!news From: CDCKAB%EMUVM1.BITNET@cunyvm.cuny.edu ( Karl Brendel) Newsgroups: comp.lang.pascal Subject: Re: Selective write within files Message-ID: <26391@adm.brl.mil> Date: 28 Mar 91 15:58:38 GMT Sender: news@adm.brl.mil Lines: 62 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 >--> > >My Question: Is there any way to just selectively take this text >file, delete 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 (""), 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 | +--------------------------------------------------------------------+