<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.eiffelroom.com" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>eiffelroom - Reading File Content - Comments</title>
 <link>http://www.eiffelroom.com/article/reading_file_content</link>
 <description>Comments for &quot;Reading File Content&quot;</description>
 <language>en</language>
<item>
 <title>Reading File Content</title>
 <link>http://www.eiffelroom.com/article/reading_file_content</link>
 <description>&lt;p&gt;In this tutorial I&#039;ll briefly explain how one can load the content of an ASCII file into a single string.&lt;/p&gt;

&lt;p&gt;The deferred class &lt;code class=&quot;geshifilter eiffel&quot;&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+FILE&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;FILE&lt;/span&gt;&lt;/a&gt;&lt;/code&gt; cannot be used directly as it is &lt;a class=&quot;&quot; style=&quot;&quot; href=&quot;/locate/deferred&quot;&gt;deferred&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The descendant &lt;code class=&quot;geshifilter eiffel&quot;&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+PLAIN_TEXT_FILE&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;PLAIN_TEXT_FILE&lt;/span&gt;&lt;/a&gt;&lt;/code&gt; is what we&#039;re actually looking for. 
&lt;h2 id=&quot;toc0&quot;&gt;Basic steps&lt;/h2&gt;
 The basic steps are the following: &lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;local&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; l_file: &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+PLAIN_TEXT_FILE&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;PLAIN_TEXT_FILE&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; l_content: &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+STRING&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;STRING&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;do&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;create&lt;/span&gt; l_file.&lt;span style=&quot;color: #000060;&quot;&gt;make_create_read_write&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;a_path&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; l_file.&lt;span style=&quot;color: #000060;&quot;&gt;read_stream&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;l_file.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; l_content := l_file.&lt;span style=&quot;color: #000060;&quot;&gt;last_string&lt;/span&gt;.&lt;span style=&quot;color: #000060;&quot;&gt;twin&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; l_file.&lt;span style=&quot;color: #000060;&quot;&gt;close&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Let&#039;s have a brief look at each single line:&lt;/p&gt;


&lt;h2 id=&quot;toc1&quot;&gt;Opening a file&lt;/h2&gt;
&lt;p&gt;We instantiate the class and open the file directly for read. Doing it like that has the advantage that it is just one line. &lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;create&lt;/span&gt; l_file.&lt;span style=&quot;color: #000060;&quot;&gt;make_create_read_write&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;a_path&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt; Note: It might happen that the postcondition is violated if you don&#039;t have the permission to do the file operation for the path specified. We will later take this into account.&lt;/p&gt;


&lt;h2 id=&quot;toc2&quot;&gt;Reading from a file&lt;/h2&gt;
 &lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;l_file.&lt;span style=&quot;color: #000060;&quot;&gt;read_stream&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;l_file.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;/div&gt; There are several other read methods available. For example you can use &lt;code class=&quot;geshifilter eiffel&quot;&gt;read_line&lt;/code&gt; to read a single line of a given file. &lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;l_content := l_file.&lt;span style=&quot;color: #000060;&quot;&gt;last_string&lt;/span&gt;.&lt;span style=&quot;color: #000060;&quot;&gt;twin&lt;/span&gt;&lt;/div&gt; As the command/query separation principle is implemented, the result is always stored in &lt;code class=&quot;geshifilter eiffel&quot;&gt;last_...&lt;/code&gt;. Reading an integer from an ASCII file would look the following:&lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;l_file.&lt;span style=&quot;color: #000060;&quot;&gt;read_integer&lt;/span&gt;&lt;br /&gt;
l_integer := l_file.&lt;span style=&quot;color: #000060;&quot;&gt;last_integer&lt;/span&gt;&lt;/div&gt; Note that we perform a call to &lt;code class=&quot;geshifilter eiffel&quot;&gt;twin&lt;/code&gt; for instances of type string as there is only one buffer per file object and it is overwritten by the next read command. If we do a &lt;code class=&quot;geshifilter eiffel&quot;&gt;twin&lt;/code&gt; we make sure that we don&#039;t have any surprising side effects later. So in general I recommend to do it for all non expanded types.
&lt;h2 id=&quot;toc3&quot;&gt;Closing a file&lt;/h2&gt;
 &lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;l_file.&lt;span style=&quot;color: #000060;&quot;&gt;close&lt;/span&gt;&lt;/div&gt; Last but not least we close the file to free system resources. However, if we simply set the reference to &lt;code class=&quot;geshifilter eiffel&quot;&gt;&lt;span style=&quot;color: #800080;&quot;&gt;Void&lt;/span&gt;&lt;/code&gt; this will do job as well, as the garbage collector calls &lt;code class=&quot;geshifilter eiffel&quot;&gt;dispose&lt;/code&gt; before he collects the object, which in turn calls &lt;code class=&quot;geshifilter eiffel&quot;&gt;close&lt;/code&gt; if the file is still open.
&lt;h2 id=&quot;toc4&quot;&gt;Better error handling&lt;/h2&gt;
 The creation feature we used so far initializes the object and tries to open the file right away. If the files does not exist an attempt to create it will be made. Which may possibly fail if you don&#039;t have the proper rights to do so. Still, the file will be empty and this is maybe not what the original intention of the programmer was.&lt;p&gt;To get more control over the whole process I recommend a different, more sophisticated way of achieving the same: &lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;local&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; l_file: &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+PLAIN_TEXT_FILE&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;PLAIN_TEXT_FILE&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;do&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;create&lt;/span&gt; l_file.&lt;span style=&quot;color: #000060;&quot;&gt;make&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;a_path&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- We perform several checks until we make a real attempt to open the file.&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;not&lt;/span&gt; l_file.&lt;span style=&quot;color: #000060;&quot;&gt;exists&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;then&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; print &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;error: &#039;&amp;quot;&lt;/span&gt; + a_path + &lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;&#039; does not exist&lt;span style=&quot;color: #005070; font-weight: bold;&quot;&gt;%N&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;else&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;not&lt;/span&gt; l_file.&lt;span style=&quot;color: #000060;&quot;&gt;is_readable&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;then&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; print &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;error: &#039;&amp;quot;&lt;/span&gt; + a_path + &lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;&#039; is not readable.&lt;span style=&quot;color: #005070; font-weight: bold;&quot;&gt;%N&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;else&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; l_file.&lt;span style=&quot;color: #000060;&quot;&gt;open_read&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; l_file.&lt;span style=&quot;color: #000060;&quot;&gt;read_stream&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;l_file.&lt;span style=&quot;color: #000060;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #800080;&quot;&gt;Result&lt;/span&gt; := l_file.&lt;span style=&quot;color: #000060;&quot;&gt;last_string&lt;/span&gt;.&lt;span style=&quot;color: #000060;&quot;&gt;twin&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; l_file.&lt;span style=&quot;color: #000060;&quot;&gt;close&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;/div&gt; This feature checks several possible error conditions and prints error messages. The pattern will be the same in most cases, but the error handling will most likely depend on you application. So you could write a feature suited for your application with proper error handling and then simply reuse that feature whenever you need to open a file. If you don&#039;t want to copy paste the code all the time and just change the two error handling lines a more fine tuned class could implement the &lt;a href=&quot;http://en.wikipedia.org/wiki/Observer_pattern&quot;&gt;observer&lt;/a&gt; pattern implemented by using &lt;a href=&quot;http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29#Agents&quot;&gt;agents&lt;/a&gt;.&lt;/p&gt;

</description>
 <comments>http://www.eiffelroom.com/article/reading_file_content#comments</comments>
 <category domain="http://www.eiffelroom.com/taxonomy/term/3">Tutorial</category>
 <category domain="http://www.eiffelroom.com/taxonomy/term/7">Beginner</category>
 <category domain="http://www.eiffelroom.com/tag/file">file</category>
 <category domain="http://www.eiffelroom.com/tag/i_o">I/O</category>
 <category domain="http://www.eiffelroom.com/tag/read">read</category>
 <pubDate>Wed, 07 Feb 2007 12:13:05 -0800</pubDate>
 <dc:creator>mtn</dc:creator>
 <guid isPermaLink="false">67 at http://www.eiffelroom.com</guid>
</item>
</channel>
</rss>
