johnbakers
johnbakers

Reputation: 24760

Progressively building a large text chunk and then writing it to a file

I am new to Common Lisp and am trying to conceive of the best way to generate a large text file. I see basically two options:

  1. Create a string and constantly add to it, then write this string to a file output stream just once, only at the final end of my iterations
  2. Create a file stream and continually write to it over and over again as the program builds the text file

Perhaps similar to 1 would be: 3. Create a string output stream and write to that, then write this stream to a new file stream.

The problem with 1 & 3 is that I don't think strings are designed to hold very large text contents, are they?

The problem with 2 is that it seems wasteful to constantly open and close a file, even if doing so conveniently with a with- command; seems inefficient and potentially error prone.

What is the typical way this is done?

Upvotes: 1

Views: 150

Answers (1)

Jerome Baldridge
Jerome Baldridge

Reputation: 528

Open the file stream and just keep it open for all your work, then close it and move onto another stream if necessary. No need to constantly reopen and close the file.

Upvotes: 1

Related Questions