Question on reading text files in Windows

Discussion in 'Ruby' started by Jim Knowlton, Feb 2, 2009.

  1. Jim Knowlton

    Jim Knowlton Guest

    I am running Ruby 1.86 on Windows, and having trouble reading in some
    text files. For some text files, if I do something simple like:

    myfile = File.open("logfile.log")
    contents = myfile.read()
    puts contents

    I get each character seperated by a space, such as:

    ”= = = V e r b o s e l o g g i n g s t a r t e d : 1 / 2 8 / 2
    0 0 9
    1 3 : 4 5 : 0 6 B u i l d t y p e : S H I P U N I C O D E

    If I bring up the file in even a bare-bones editor (such as VIM), I
    get the file as it normally is (without any extraneous spaces). Does
    anyone know why this would be, or how I can work around it? It's
    causing issues as I am trying to write a script to search for a
    particular string of text, and obviously it isn't found, even though
    it should be.

    Thanks,

    Jim
    Jim Knowlton, Feb 2, 2009
    #1
    1. Advertising

  2. Jim Knowlton

    Stefan Lang Guest

    2009/2/2 Jim Knowlton <>:
    > I am running Ruby 1.86 on Windows, and having trouble reading in some
    > text files. For some text files, if I do something simple like:
    >
    > myfile =3D File.open("logfile.log")
    > contents =3D myfile.read()
    > puts contents
    >
    > I get each character seperated by a space, such as:
    >
    > =94=3D =3D =3D V e r b o s e l o g g i n g s t a r t e d : 1 / 2 =

    8 / 2
    > 0 0 9
    > 1 3 : 4 5 : 0 6 B u i l d t y p e : S H I P U N I C O D E
    >
    > If I bring up the file in even a bare-bones editor (such as VIM), I
    > get the file as it normally is (without any extraneous spaces). Does
    > anyone know why this would be, or how I can work around it? It's
    > causing issues as I am trying to write a script to search for a
    > particular string of text, and obviously it isn't found, even though
    > it should be.


    The file is probably UTF-16 encoded and starts with a BOM.
    Try to convert the string to UTF-8, or switch to Ruby 1.9.

    Stefan
    Stefan Lang, Feb 2, 2009
    #2
    1. Advertising

  3. Jim Knowlton

    Stefan Lang Guest

    2009/2/2 Stefan Lang <>:
    > 2009/2/2 Jim Knowlton <>:
    >> I am running Ruby 1.86 on Windows, and having trouble reading in some
    >> text files. For some text files, if I do something simple like:
    >>
    >> myfile =3D File.open("logfile.log")
    >> contents =3D myfile.read()
    >> puts contents
    >>
    >> I get each character seperated by a space, such as:
    >>
    >> =94=3D =3D =3D V e r b o s e l o g g i n g s t a r t e d : 1 / 2=

    8 / 2
    >> 0 0 9
    >> 1 3 : 4 5 : 0 6 B u i l d t y p e : S H I P U N I C O D E
    >>
    >> If I bring up the file in even a bare-bones editor (such as VIM), I
    >> get the file as it normally is (without any extraneous spaces). Does
    >> anyone know why this would be, or how I can work around it? It's
    >> causing issues as I am trying to write a script to search for a
    >> particular string of text, and obviously it isn't found, even though
    >> it should be.

    >
    > The file is probably UTF-16 encoded and starts with a BOM.
    > Try to convert the string to UTF-8, or switch to Ruby 1.9.


    Sorry, I meant to say "Try to convert the string to UTF-8 WITH Iconv"

    Stefan
    Stefan Lang, Feb 2, 2009
    #3
  4. Jim Knowlton

    Jim Knowlton Guest

    Thanks for the pointer! I actually ended up using the iconv module,
    and it worked like a charm. Incidentally, in case anyone else is
    curious about this, Windows .REG files get saved as UTF-16 by default.
    Jim Knowlton, Feb 2, 2009
    #4
    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.

Share This Page