# Question on reading text files in Windows

**URL:** <https://www.ruby-forum.com/t/question-on-reading-text-files-in-windows/159247>\
**Category:** Ruby\
**Created:** [February 2, 2009, 6:40pm UTC](https://www.ruby-forum.com/t/question-on-reading-text-files-in-windows/159247 "2009-02-02T18:40:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigjimmyk3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6de8d8/32.png) [@bigjimmyk3](https://www.ruby-forum.com/u/bigjimmyk3)\
**Post date:** [February 2, 2009, 6:40pm UTC](https://www.ruby-forum.com/t/question-on-reading-text-files-in-windows/159247/1 "2009-02-02T18:40:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bigjimmyk3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6de8d8/32.png) [@bigjimmyk3](https://www.ruby-forum.com/u/bigjimmyk3)\
**Post date:** [February 2, 2009, 8:09pm UTC](https://www.ruby-forum.com/t/question-on-reading-text-files-in-windows/159247/2 "2009-02-02T20:09:58Z")

</div>

2009/2/2 Jim K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![bigjimmyk3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6de8d8/32.png) [@bigjimmyk3](https://www.ruby-forum.com/u/bigjimmyk3)\
**Post date:** [February 2, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/question-on-reading-text-files-in-windows/159247/3 "2009-02-02T20:26:27Z")

</div>

2009/2/2 Stefan L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > ”= = = 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  
> > 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

---

<div class="post-metadata">

**Author:** ![bigjimmyk3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6de8d8/32.png) [@bigjimmyk3](https://www.ruby-forum.com/u/bigjimmyk3)\
**Post date:** [February 2, 2009, 10:12pm UTC](https://www.ruby-forum.com/t/question-on-reading-text-files-in-windows/159247/4 "2009-02-02T22:12:58Z")

</div>

Thanks…so if I upgraded to Ruby 1.9, would it convert it  
automatically?

---

<div class="post-metadata">

**Author:** ![bigjimmyk3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6de8d8/32.png) [@bigjimmyk3](https://www.ruby-forum.com/u/bigjimmyk3)\
**Post date:** [February 2, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/question-on-reading-text-files-in-windows/159247/5 "2009-02-02T22:45:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bigjimmyk3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6de8d8/32.png) [@bigjimmyk3](https://www.ruby-forum.com/u/bigjimmyk3)\
**Post date:** [February 2, 2009, 10:54pm UTC](https://www.ruby-forum.com/t/question-on-reading-text-files-in-windows/159247/6 "2009-02-02T22:54:08Z")

</div>

2009/2/2 Jim K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thanks…so if I upgraded to Ruby 1.9, would it convert it  
> automatically?

You’d have to tell it that you want to work with UTF-8  
internally by putting this at the top of your application:

```
Encoding.default_internal = Encoding::UTF_8

```

and then tell the read or open function that the file  
is UTF-16 encoded, e.g.:

```
content = File.read("logfile.log", encoding: "utf-16")

```

Though I don’t know how many gems already work for  
Ruby 1.9.1 on Windows.

Stefan
