Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!snorkelwacker.mit.edu!bloom-beacon!eru!kth.se!sunic!mcsun!hp4nl!phigate!ehviea!sun4dts!derek From: derek@sun4dts.dts.ine.philips.nl (derek) Newsgroups: comp.lang.pascal Subject: Re: Selective write within files Message-ID: <705@sun4dts.dts.ine.philips.nl> Date: 28 Mar 91 13:51:15 GMT References: <1991Mar27.160905.1962@cs.mcgill.ca> Lines: 41 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 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.