Bug #9164

IO::foreach incorrect work for binary file on windows platform

Added by wind winy 5 months ago. Updated 5 months ago.

[ruby-core:58629]
Status:Rejected
Priority:Normal
Assignee:-
Category:-
Target version:-
ruby -v:2.0.0p353,2.1.0dev Backport:1.9.3: UNKNOWN, 2.0.0: UNKNOWN

Description

d2 = "123\n".b + "\x1a\xe8\xba\b".b + "\n789\n".b

File.binwrite('1.txt', d2)

p File.binread('1.txt').each_line.to_a # => ["123\n", "\x1A\xE8\xBA\b\n", "789\n"]



p File.foreach('1.txt').to_a           # => ["123\n"]

Encoding.default_external = Encoding::UTF_8
p File.foreach('1.txt').to_a           # => ["123\n"]

Encoding.default_external = Encoding::ASCII_8BIT
p File.foreach('1.txt').to_a           # => ["123\n"]

run above code on windows, File.foreach.to_a just return 1 row.
run on mac and linux, that's correct, return 3 rows.

History

#1 Updated by Heesob Park 5 months ago

On Windows, "\x1A"(Control+Z) means end of file.
You must use binary read mode.

p File.foreach('1.txt',mode:'rb').to_a => ["123\n", "\x1A\xE8\xBA\b\n", "789\n"]

#2 Updated by wind winy 5 months ago

phasis68 (Heesob Park) wrote:

On Windows, "\x1A"(Control+Z) means end of file.
You must use binary read mode.

p File.foreach('1.txt',mode:'rb').to_a => ["123\n", "\x1A\xE8\xBA\b\n", "789\n"]

Thank you.

Sorry, my problem, it isn't a bug.

#3 Updated by Motohiro KOSAKI 5 months ago

  • Description updated (diff)
  • Status changed from Open to Rejected

Also available in: Atom PDF