Project

General

Profile

Bug #9164

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

Added by wind winy about 3 years ago. Updated about 3 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
ruby -v:
2.0.0p353,2.1.0dev
[ruby-core:58629]

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 [ruby-core:58630] Updated by Heesob Park about 3 years 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 [ruby-core:58637] Updated by wind winy about 3 years 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 [ruby-core:58650] Updated by Motohiro KOSAKI about 3 years ago

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

Also available in: Atom PDF