# Reading in a file

**URL:** <https://www.ruby-forum.com/t/reading-in-a-file/122768>\
**Category:** Ruby\
**Created:** [December 11, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/reading-in-a-file/122768 "2007-12-11T23:08:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [December 11, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/reading-in-a-file/122768/1 "2007-12-11T23:08:00Z")

</div>

hi,

i want to create a ruby program that uses a “database” of reference  
specifications held in a text file to process a set of other files that  
cite these references, substituting correct citations in the text and  
adding a reference section to the text after the last file has been  
processed. All the output should be sent to the standard output (except  
errors which should go to standard error). To format the references i  
need to use HTML tags. Some useful tags :

**bold** _italic_ underline superscript

i cant find out how to read in the file. where should i save the text  
file on the computer? and where will the new file appear?

thanks

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [December 12, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/reading-in-a-file/122768/2 "2007-12-12T15:58:58Z")

</div>

Chris Daves wrote:

> i want to create a ruby program that uses a “database” of reference  
> specifications held in a text file to process a set of other files that  
> cite these references, substituting correct citations in the text and  
> adding a reference section to the text after the last file has been  
> processed. All the output should be sent to the standard output (except  
> errors which should go to standard error). To format the references i  
> need to use HTML tags. Some useful tags :
> 
> **bold** _italic_ underline superscript
> 
> i cant find out how to read in the file. where should i save the text  
> file on the computer? and where will the new file appear?

Answer here:  
[http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/8aae24bd9706805f#](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/8aae24bd9706805f#)

I suggest you stop using ruby-forum until it is fixed, and instead use  
another source that lets you see the answers people are providing.

---

<div class="post-metadata">

**Author:** ![chrisdwan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@chrisdwan](https://www.ruby-forum.com/u/chrisdwan)\
**Post date:** [December 12, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/reading-in-a-file/122768/3 "2007-12-12T19:26:00Z")

</div>

Chris Daves wrote:

> **bold** _italic_ underline superscript
> 
> i cant find out how to read in the file. where should i save the text  
> file on the computer? and where will the new file appear?
> 
> thanks

Look at the doc for the File class and the IO class. File inherits from  
IO so all the methods in IO are available in File.

You can save the text file anywhere. When you open the file for reading  
you have to tell Ruby - actually the File.open method - where it is. For  
example if you save the file in “C:\My Documents\references.txt” then  
you have to tell File.open to open “C:\My Documents\references.txt”.  
File.open can open any disk file, as long as you tell it which one to  
open.

“Standard output” is the console by default, so since you’re writing the  
new file to standard output, the new file will appear on your console  
unless you redirect standard output to a file, in which case the new  
file will appear where ever you redirected standard output. Redirecting  
is a function of the command processor or shell, not Ruby. Typically you  
redirect standard output by using “\>filename” on the command line. For  
example, the following command redirects standard output to a file  
called "myoutput.txt in the current directory:

ruby mypgm.rb \>myoutput.txt

Ruby uses special variables to that represent standard output and  
standard error. See the doc on $stdout and $stderr.
