Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Reid G.C.Thinking in PostScript.1990.pdf
Скачиваний:
17
Добавлен:
23.08.2013
Размер:
846.44 Кб
Скачать

string print

write string to stdout output file

any =

convert any to a string and write

 

string to stdout file, followed by a

 

newline

any1... anyn stack

print contents of stack

any ==

convert any to a readable

 

representation and write to stdout

 

(works like =, but is more powerful),

 

followed by a newline

any1... anyn pstack

print contents of stack non-

 

destructively

prompt

used only in interactive (executive)

 

mode

echo

used only in interactive (executive)

 

mode

NOTE: Operators marked by a dagger are not available on all implementations (they are language extensions).

OPENING AND CLOSING FILES

To open a file in PostScript, you use the file operator (which you can think of as an “open” operator, for all practical purposes). Opening a file gives you a file object that you can then use for reading from or writing to that file. The only tricky part is to make sure that there really is a file to open.

The file operator requires a file name and a mode or permission string. The file name is obvious. The mode is how you specify that you want to open the file for reading, writing, or (in some implementations) appending. If you open a file for writing (not appending), it will instantly create a file of zero length, destroying any other file that might have existed with that name. Because of this you should be especially careful when you open files for writing the first few times.

Let’s look at the syntax of the file operator a little more closely.

filename mode file file_object

The exact syntax of the filename operand depends, to some extent, on the environment in which the interpreter is running (see Table 14.2).

170

Chapter 14: USING FILES AND INPUT/OUTPUT TECHNIQUES

Table 14.2: Example File Names in Different Environments

Environment

Example File Name

UNIX

(/LocalLibrary/fonts/outline/Palatino-Roman)

Macintosh

(:root:System Folder:PalatRom)

DOS

(D:\FONTS\PALATROM.FNT)

PostScript

(fonts/Palatino-Roman)

You will have to find some documentation for your system that details the syntax for file names.

Table 14.3: Modes for Opening Files with file Operator

Mode

Explanation

(r)

open file for reading only

(w)open file for writing and delete any existing contents of file

The best way to make sure that the file name you are about to use is valid is to use the status operator first (see Example 14.8 later in this chapter).

Example 14.1 shows some examples of the file operator in use, to give you the flavor of some file names and modes:

Example 14.1: Sample Use of the file Operator

%NOTE: this is not a working program, just several individual examples

%of using the file operator

(/etc/passwd) (r) file (fonts/Palatino-Roman) (w) file (%stderr) (w) file

(%stdin) (r) file

Once your file is open, you can read from it (if you opened it with a mode for reading) or write to it, as detailed in the next section. Don’t forget to close it properly (with closefile) when you’re done (Example 14.2).

Example 14.2: Closing a File

/file_object (/etc/passwd) (r) file def % ...

file_object closefile

Chapter 14: USING FILES AND INPUT/OUTPUT TECHNIQUES

171