Dude, where's my carriage returns?

Discussion in 'Ruby' started by Karl von Laudermann, Aug 19, 2005.

  1. I'm trying to write a tiny Ruby program that will print the ascii
    values of all characters in a text file. I'm doing this to help debug a
    bash script, which runs under Cygwin on Windows, that makes changes to
    a text file, but I suspect isn't adding carriage returns before each
    line feed. My Ruby program looks like this:

    --------------
    fn = ARGV[0]
    count = 0

    File.open(fn, "r") do |f|
    f.each_byte do |ch|
    print "#{ch} "
    count += 1
    end
    end

    puts
    puts "Count = #{count}"
    -------------

    When I run it on a small text file that looks like this...

    -------------
    hello
    world
    -------------

    ....I get the following output:

    -------------
    104 101 108 108 111 10 119 111 114 108 100
    Count = 11
    -------------

    However, since I'm creating this text file in Windows Notepad, I know
    that there should be a carriage return before the line feed, and the
    output *should* actually be:

    -------------
    104 101 108 108 111 13 10 119 111 114 108 100
    Count = 12
    -------------

    Note the 13 (CR) before the 10 (LF).

    If I use Windows to look at the file properties, it confirms that the
    file is actually 12 bytes in size. If I add more line breaks, the file
    size increases by two for each one, while the "Count" value in the Ruby
    script's output increases by only one byte for each, and no byte of
    value 13 is ever displayed.

    How can I get Ruby to actually read carriage return characters from a
    file?
    Karl von Laudermann, Aug 19, 2005
    #1
    1. Advertising

  2. Hi Karl,

    Try this:

    --- Karl von Laudermann <> wrote:

    <snip>
    =20
    > File.open(fn, "r") do |f|

    ^^^^
    <snip>

    File.open(fn, "rb") do |f|


    HTH,
    -- shanko


    =09
    __________________________________=20
    Yahoo! Mail for Mobile=20
    Take Yahoo! Mail with you! Check email on your mobile phone.=20
    http://mobile.yahoo.com/learn/mail=20
    Shashank Date, Aug 19, 2005
    #2
    1. Advertising

  3. On Aug 19, 2005, at 2:21 PM, Karl von Laudermann wrote:

    > fn = ARGV[0]
    > count = 0
    >
    > File.open(fn, "r") do |f|


    File.open(fn, "rb") do |f|

    > f.each_byte do |ch|
    > print "#{ch} "
    > count += 1
    > end
    > end
    >
    > puts
    > puts "Count = #{count}"


    Try the above fix to open the file in "binary" mode, shutting off
    line-ending translation.

    James Edward Gray II
    James Edward Gray II, Aug 19, 2005
    #3
  4. Shashank Date wrote:
    >
    > Try this:
    >
    > File.open(fn, "rb") do |f|


    James Edward Gray II wrote:
    >
    > File.open(fn, "rb") do |f|


    Perfect! Thanks, both of you.
    Karl von Laudermann, Aug 19, 2005
    #4
  5. First guess:

    File.open(fn, "rb") do |f|

    cheers

    Simon

    Karl von Laudermann wrote:
    > I'm trying to write a tiny Ruby program that will print the ascii
    > values of all characters in a text file. I'm doing this to help debug a
    > bash script, which runs under Cygwin on Windows, that makes changes to
    > a text file, but I suspect isn't adding carriage returns before each
    > line feed. My Ruby program looks like this:
    >
    > --------------
    > fn = ARGV[0]
    > count = 0
    >
    > File.open(fn, "r") do |f|
    > f.each_byte do |ch|
    > print "#{ch} "
    > count += 1
    > end
    > end
    >
    > puts
    > puts "Count = #{count}"
    > -------------
    >
    > When I run it on a small text file that looks like this...
    >
    > -------------
    > hello
    > world
    > -------------
    >
    > ....I get the following output:
    >
    > -------------
    > 104 101 108 108 111 10 119 111 114 108 100
    > Count = 11
    > -------------
    >
    > However, since I'm creating this text file in Windows Notepad, I know
    > that there should be a carriage return before the line feed, and the
    > output *should* actually be:
    >
    > -------------
    > 104 101 108 108 111 13 10 119 111 114 108 100
    > Count = 12
    > -------------
    >
    > Note the 13 (CR) before the 10 (LF).
    >
    > If I use Windows to look at the file properties, it confirms that the
    > file is actually 12 bytes in size. If I add more line breaks, the file
    > size increases by two for each one, while the "Count" value in the Ruby
    > script's output increases by only one byte for each, and no byte of
    > value 13 is ever displayed.
    >
    > How can I get Ruby to actually read carriage return characters from a
    > file?
    >
    >
    >
    Simon Kröger, Aug 19, 2005
    #5
  6. Simon Kr=F6ger wrote:

    > First guess:
    >=20
    > File.open(fn, "rb") do |f|
    >=20
    > cheers
    >=20
    > Simon


    rofl, as always James was quicker :)

    cheers

    Simon
    Simon Kröger, Aug 19, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard
    Replies:
    8
    Views:
    567
    Richard
    Feb 7, 2005
  2. Guttyguppy

    New xml dude needs help with xslt

    Guttyguppy, Sep 15, 2005, in forum: XML
    Replies:
    6
    Views:
    378
  3. Merrill & Michele

    Dude and Duden

    Merrill & Michele, Dec 10, 2004, in forum: C Programming
    Replies:
    6
    Views:
    371
    Merrill & Michele
    Dec 13, 2004
  4. Pistorius

    Hyperlink dude!!

    Pistorius, Dec 23, 2009, in forum: .NET
    Replies:
    1
    Views:
    315
    Pistorius
    Dec 23, 2009
  5. Steve Anderson
    Replies:
    3
    Views:
    230
    Steve Anderson
    Jun 21, 2004
Loading...

Share This Page