
In this tutorial I'll briefly explain how one can load the content of an ASCII file into a single string.
The deferred class FILE cannot be used directly as it is deferred.
The descendant PLAIN_TEXT_FILE is what we're actually looking for.
Contents
|
Basic steps
The basic steps are the following:
local l_file: PLAIN_TEXT_FILE l_content: STRING do create l_file.make_create_read_write (a_path) l_file.read_stream (l_file.count) l_content := l_file.last_string.twin l_file.close end
Let's have a brief look at each single line:
Opening a file
We instantiate the class and open the file directly for read. Doing it like that has the advantage that it is just one line.
create l_file.make_create_read_write (a_path)
Reading from a file
l_file.read_stream (l_file.count)
read_line to read a single line of a given file.
l_content := l_file.last_string.twin
last_....
Reading an integer from an ASCII file would look the following:\
l_file.read_integer l_integer := l_file.last_integer
twin 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 twin we make sure that we don't have any surprising side effects later. So in general I recommend to do it for all non expanded types.
Closing a file
l_file.closeVoid this will do job as well, as the garbage collector calls dispose before he collects the object, which in turn calls close if the file is still open.
Better error handling
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'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.
To get more control over the whole process I recommend a different, more sophisticated way of achieving the same:
local l_file: PLAIN_TEXT_FILE do create l_file.make (a_path) -- We perform several checks until we make a real attempt to open the file. if not l_file.exists then print ("error: '" + a_path + "' does not exist%N") else if not l_file.is_readable then print ("error: '" + a_path + "' is not readable.%N") else l_file.open_read l_file.read_stream (l_file.count) Result := l_file.last_string.twin l_file.close end end end