7.5.1 Using the file Fragment

A program using the file fragment will have the following structure:

INCLUDE '~beta/basiclib/file
--- program: descriptor ---
(# ...
   f: @file;
do ...
   'Hello world' -> f.putline;
...
#)

7.5.2 Using diskEntry

An example showing the use of the path attribute of DiskEntry: The path specified on the command line is decomposed, and the various parts of it are printed:

Program 8: decompose.bet

ORIGIN '~beta/basiclib/file';

--- program: descriptor ---
(* An example showing the use of the 'path' attribute of DiskEntry: The path
 * specified on the command line is decomposed, and the various parts of it are
 * printed.
 *)

(# e: @diskentry;
do (if noOfArguments <> 2 then
       'Usage: ' -> puttext; 1->arguments->puttext; ' path' -> putline;
       stop;
   if);
   2 -> arguments -> e.path;
   'The path \''->puttext; 
   e.path -> puttext;
   '\' is composed like this:'->putline;
   'Head of path:      '->puttext; e.path.head -> putline;
   'Name:              '->puttext; e.path.name -> putline;
   'Prefix of name:    '->puttext; e.path.name.prefix -> putline;
   'Suffix of name:    '->puttext; e.path.name.suffix -> putline;
   'Extension of name: '->puttext; e.path.name.extension -> putline;
   (if e.exists then '(and the entry exists on disk)' -> putline;
    else '(there is no such entry on disk)' -> putline;
   if);
#)

7.6 Using File

Program showing the use of exceptions in files: if the input file specified on the command line cannot be opened, the exception noSuchFileError is raised. In this example, it is further bound specifying that the program should continue after the exception is raised. Then a new file name is prompted for. Instead of this approach, of course, the attributes exists of diskEntry could have been used.

If the output file could not be opened, the exception noSpaceError is raised, and this exception is further bound to print a message:

Program 9: fileerror.bet

ORIGIN '~beta/basiclib/file';
--- program: descriptor ---

(* Program showing the use of exceptions in files: if the input file
 * specified on the command line cannot be opened, the exception
 * NoSuchFileError is raised. In this example, it is further bound
 * specifying that the program should continue after the exception is raised.
 * Then a new file name is prompted for.
 * Instead of this approach, of course, the attributes 'exists' of DiskEntry
 * could have been used.
 * If the output file could not be opened, the exception NoSpaceError is raised,
 * and this exception is further bound to print a message.
 *)
 
(# outFile: @file
     (# NoSpaceError ::< 
          (# do 'It is time to delete garbage!' -> msg.putLine #); 
     #);     
   inFile: @ file 
     (# NoSuchFileError ::< (# do true -> continue; false -> OK #); 
     #);
   OK: @ boolean; 
   
do (if noOfArguments <> 2 then
       'Usage: ' -> puttext; 1->arguments->puttext; ' out-file' -> putline;
       stop;
   if);
   2 -> arguments -> outFile.name;
   outFile.OpenWrite;
   
   'nofile.bet' -> inFile.name;
   true -> OK;
   openFile: 
     (# 
     do inFile.OpenRead;
        (if not OK then
            'File does not exist: ' ->  screen.puttext;
            infile.name -> putline;
            'Type input file name: ' ->  screen.putText;
            inFile.name.read;
            true -> OK;
            restart openFile
        if);
     #);
   
   readFile: 
     (# 
     do (if not inFile.eos then 
            inFile.getatom -> outFile.putText;
            outFile.newLine;
            restart readFile
            else leave readFile
        if)
     #);
   inFile.close;
   outFile.close;
   'Tokens from \''->puttext; infile.name -> puttext;
   '\' copied to \''->puttext; outfile.name -> puttext;
   '\''->putline;
#)


Basic Libraries - Reference Manual
© 1990-2002 Mjølner Informatics
[Modified: Friday November 10th 2000 at 14:53]