[fpc-pascal] Replacing line in big text file

Tomas Hajny XHajT03 at hajny.biz
Mon Oct 8 17:15:15 CEST 2012


On Mon, October 8, 2012 17:00, Guionardo Furlan wrote:
> 2012/10/8 Krzysztof <dibo20 at wp.pl>:


Hi,

>> I must modify some one line (mostly first line) in big text file (~10
>> 000 lines). Doing this by loading and saving file using TStringList
>> takes a long time (and amount of RAM). Exists any faster way based on
>> TextFile type (like Write, WriteLn)?
> If the line lenght has the same size, you can use a simple file of
> char to locate initial and ending offsets of the line and directly
> edit chars. I think this is the fastest way.

Depending on the underlying platform and its caching capabilities, you may
want to use some caching mechanism even in that case to avoid reading the
file one character by another.

Especially if the size of the replaced text isn't equal to the original,
you can have a look at my unit buffered
(http://www.volny.cz/xhajt03/buffered.zip). The published version doesn't
support ansistrings, but it can still handle lines with more than 255
characters (as supported by standard shortstrings). As an example, I've
used it for modifying (broken) headers in e-mail messages which is fairly
similar to your case, especially if it includes e-mail messages with large
attachments (if you like, I can share this example with you too).

Tomas





More information about the fpc-pascal mailing list