spit does not truncate, leaves tail of original file if output is shorter length.

Description

For example:

(spit "c:/tmp/spit.txt" "012345")
(spit "c:/tmp/spit.txt" "BB")

Results in a file containing: BB345.

Reported via email by cees van Kemenade.

Environment

None

Activity

Show:
David Miller
November 28, 2014, 4:27 PM

spit with a string argument defaults to opening a FileStream (wrapped in a StreamWriter) in mode FileMode.CreateOrNew. This will work whether or not the file exists. To default to Truncate will fail if the file does not exist. For this reason, I don't want to make it the default. I'm also not sure how to play with tests of file existence versus given file-mode to determine what action to take.

If you know the file exists and want to truncate, call spit with that file mode.

(spit "filename" "test" :file-mode System.IO.FileMode/Truncate)

David Miller
November 28, 2014, 4:28 PM

Correct action is to call spit specifying the file mode:

(spit "c:/tmp/spit.txt" "BB" :file-mode System.IO.FileMode/Truncate)

David Miller
March 20, 2019, 12:08 AM

At the request of the OP, I have decided to reconsider this issue.

David Miller
March 20, 2019, 12:11 AM

Cees suggests the attached code.

David Miller
March 20, 2019, 12:14 AM

Adding to this: change slurp to read the file in non-locking mode.

Assignee

David Miller

Reporter

David Miller

Labels

None

Approval

None

Patch

None

Priority

Major
Configure