Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-10

Replacing line in big text file

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Krzysztof

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)?

Regards

Reply by email

Guionardo Furlan
to Krzysztof

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.

2012/10/8 Krzysztof :

Show quoted text 11 lines
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)? Regards _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Timeo hominem unius libri Cogito ergo sum - Carpe diem []s Guionardo Furlan http://guionardo.blogspot.com http://www.guionardofurlan.com.br

Reply by email

Tomas Hajny
to Guionardo Furlan
Guionardo Furlan wrote
2012/10/8 Krzysztof :

Hi,

Show quoted text 7 lines
> 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

Reply by email