>>This being VFP9, we don't care if aLines() returns more than 65000, so that's what I'd do. Get the whole thing into an array, then output lines one by one, inserting the headers as needed.
>
>Still I would expect this be slow on a big (huge) files.
Yes, but 4 MB is not big :).
On a file that's about 2M long,
? SECONDS(), ALINES(aa, FILETOSTR("my_long_program.prg")), SECONDS()
returns 47069 lines in 0.335 seconds. Since I didn't have a larger file at hand,
? SECONDS(), ALINES(aa, REPLICATE(FILETOSTR("my_long_program.prg"),4)), SECONDS()
gives only .377 seconds (probably due to caching), and I had about a dozen other windows open and my machine is no Ferrari. So it's only the time it takes to fwrite() everything, which is a low level operation which is as fast as it gets.