# Data loss when writing long lines with File.print after File.set\_encoding

**URL:** <https://forum.crystal-lang.org/t/data-loss-when-writing-long-lines-with-file-print-after-file-set-encoding/8830>\
**Category:** Help & Support\
**Created:** [March 30, 2026, 1:42am UTC](https://forum.crystal-lang.org/t/data-loss-when-writing-long-lines-with-file-print-after-file-set-encoding/8830 "2026-03-30T01:42:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bloovis](https://avatars.discourse-cdn.com/v4/letter/b/a5b964/32.png) [@bloovis](https://forum.crystal-lang.org/u/bloovis)\
**Post date:** [March 30, 2026, 1:42am UTC](https://forum.crystal-lang.org/t/data-loss-when-writing-long-lines-with-file-print-after-file-set-encoding/8830/1 "2026-03-30T01:42:56Z")

</div>

I ran into an interesting problem with my Crystal text editor. It seems that writing long lines to a file can cause characters to be dropped in some situations. Specifically, if I use File.set\_encoding and set invalid to :skip, then File.print of a very long line line causes bytes to dropped at 1K offsets. This 1K oddity leads me to think there’s a buffer boundary problem somewhere in File.print.

I’m using Crystal 1.18.2 on Fedora Linux 43 (x86\_64).

Here is a test program that demonstrates the problem: when I run it, it generates a file containing 2999 bytes, when it should be 3001 bytes. When I delete the set\_encoding line, the problem goes away.

```auto
longline = "0123456789" * 300
File.open("junk", "w") do |f|
  f.set_encoding("UTF-8", invalid: :skip) # This causes the data loss.
  f.print(longline)
  f.print("\n")
end
size = File.info("junk").size
puts "file junk has #{size} bytes, should be 3001"

```

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [March 30, 2026, 3:31am UTC](https://forum.crystal-lang.org/t/data-loss-when-writing-long-lines-with-file-print-after-file-set-encoding/8830/2 "2026-03-30T03:31:28Z")

</div>

It’s not just files, it happens with `IO::Memory`, too. In this example, all of the `|` characters are filtered from the buffer:

```crystal
longline = ("." * 1024 + "|") * 4
buffer = IO::Memory.new
buffer.set_encoding "UTF-8", invalid: :skip
buffer << longline

puts buffer

puts "buffer has #{buffer.bytesize} bytes, should be 4100"

```

This seems to be a bug. Can you [file an issue](https://github.com/crystal-lang/crystal/issues/new/choose) on the GitHub repo?

---

<div class="post-metadata">

**Author:** ![bloovis](https://avatars.discourse-cdn.com/v4/letter/b/a5b964/32.png) [@bloovis](https://forum.crystal-lang.org/u/bloovis)\
**Post date:** [March 30, 2026, 4:03am UTC](https://forum.crystal-lang.org/t/data-loss-when-writing-long-lines-with-file-print-after-file-set-encoding/8830/3 "2026-03-30T04:03:41Z")

</div>

Thank you, that is a better test program. I filed a bug and included your program.

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [March 30, 2026, 4:31am UTC](https://forum.crystal-lang.org/t/data-loss-when-writing-long-lines-with-file-print-after-file-set-encoding/8830/4 "2026-03-30T04:31:44Z")

</div>

Added [a PR](https://github.com/crystal-lang/crystal/pull/16797) for it. It may not be the right solution, but at the very least it points out where the problem is.
