Actions
Bug #9164
closedIO::foreach incorrect work for binary file on windows platform
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.
Updated by phasis68 (Heesob Park) almost 11 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"]
Updated by windwiny (wind winy) almost 11 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.
Updated by kosaki (Motohiro KOSAKI) almost 11 years ago
- Description updated (diff)
- Status changed from Open to Rejected
Actions
Like0
Like0Like0Like0