Forum: Ruby-core [ruby-trunk - Bug #9164][Open] IO::foreach incorrect work for binary file on windows platform

B9aafb646d887f93000d9e8a5a214d6c?d=identicon&s=25 windwiny (wind winy) (Guest)
on 2013-11-27 07:25
(Received via mailing list)
Issue #9164 has been reported by windwiny (wind winy).

----------------------------------------
Bug #9164: IO::foreach incorrect work for binary file on windows
platform
https://bugs.ruby-lang.org/issues/9164

Author: windwiny (wind winy)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: 2.0.0p353,2.1.0dev
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN



    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.
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2013-11-27 08:05
(Received via mailing list)
Issue #9164 has been updated by phasis68 (Heesob Park).


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"]
----------------------------------------
Bug #9164: IO::foreach incorrect work for binary file on windows
platform
https://bugs.ruby-lang.org/issues/9164#change-43199

Author: windwiny (wind winy)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: 2.0.0p353,2.1.0dev
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN



    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.
B9aafb646d887f93000d9e8a5a214d6c?d=identicon&s=25 windwiny (wind winy) (Guest)
on 2013-11-27 11:30
(Received via mailing list)
Issue #9164 has been updated by windwiny (wind winy).


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.
----------------------------------------
Bug #9164: IO::foreach incorrect work for binary file on windows
platform
https://bugs.ruby-lang.org/issues/9164#change-43208

Author: windwiny (wind winy)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: 2.0.0p353,2.1.0dev
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN



    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.
02da662c083396641da96c1d32fc86ed?d=identicon&s=25 kosaki (Motohiro KOSAKI) (Guest)
on 2013-11-28 00:46
(Received via mailing list)
Issue #9164 has been updated by kosaki (Motohiro KOSAKI).

Description updated
Status changed from Open to Rejected


----------------------------------------
Bug #9164: IO::foreach incorrect work for binary file on windows
platform
https://bugs.ruby-lang.org/issues/9164#change-43218

Author: windwiny (wind winy)
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


    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.
This topic is locked and can not be replied to.