
- •Preface
- •DESIGN FEATURES
- •STRUCTURED PROGRAMMING TECHNIQUES
- •PROGRAMMING TASKS
- •WINDOW SYSTEMS, COMMUNICATIONS, AND DISPLAYS
- •DATA STRUCTURES AND ALGORITHMS
- •CONCLUDING THOUGHTS
- •PostScript is Not Like C
- •COMPARISON OF LANGUAGE MECHANISMS
- •EXPRESSING AN ALGORITHM AS A PROGRAM
- •THE UNIX SHELL AND OPERATING SYSTEM
- •INPUT, OUTPUT, AND THROUGHPUT
- •CONCLUDING THOUGHTS
- •Foundations
- •POSTSCRIPT LANGUAGE SYNTAX
- •SIMPLE PROGRAM STRUCTURE
- •Make Definitions First
- •Indentation Style
- •SETTING UP TEMPLATES
- •DECLARING AND USING VARIABLES
- •Arithmetic with Numeric Variables
- •Using the // Notation for Constants
- •ALLOCATING MEMORY
- •GETTING MEMORY BACK
- •OPENING AND CLOSING FILES
- •COMPARISONS AND EQUALITY OF OBJECTS
- •CONCLUDING THOUGHTS
- •Some Typical Programs
- •A TYPICAL PAGE DESCRIPTION PROGRAM
- •FONT PROGRAMS
- •PROGRAMS THAT READ DATA
- •QUERY PROGRAMS
- •ENCAPSULATED POSTSCRIPT PROGRAMS
- •PERSISTENTLY RESIDENT PROGRAMS
- •CONCLUDING THOUGHTS
- •Understanding the Stack
- •A QUICK OVERVIEW OF DATA TYPES
- •NAME LOOKUP
- •HOW OPERATORS USE THE STACK
- •GROUPING AND VISUAL CHUNKING
- •THINKING BACKWARD AND SIDEWAYS
- •COMPOSITE OBJECTS
- •THE OTHER STACKS
- •The Dictionary Stack
- •The Execution Stack
- •The Graphics State Stack
- •CONCLUDING THOUGHTS
- •Trusting the Stack
- •SAFETY OF DATA ON THE STACK
- •WHERE ARE THE DATA GOING?
- •REARRANGING THE STACK
- •Using the dup and index Operators
- •Using the roll Operator
- •CONDITIONALS AND LOOPS
- •RECURSION AND LOCAL VARIABLES
- •CONCLUDING THOUGHTS
- •Building Conditional Statements
- •SIMPLE CONDITIONALS
- •SETTING UP THE CONDITION
- •CONDITIONALS ARE NOT MAGIC
- •NESTED CONDITIONALS AND ELSE CLAUSES
- •COMPOUND CONDITIONALS
- •CONCLUDING THOUGHTS
- •Using Looping Constructs
- •LOOP BASICS
- •USING THE LOOP INDEX
- •LOOPS ARE PROCEDURE BODIES
- •LOOPS OF INSTRUCTIONS
- •EXITING LOOPS PREMATURELY
- •CONCLUDING THOUGHTS
- •Procedures
- •WHAT EXACTLY IS A PROCEDURE?
- •PARAMETER PASSING
- •CONSTRUCTING GOOD PROCEDURES
- •What to Name Your Procedure
- •A Useful Naming Convention
- •SELF-MODIFYING PROCEDURES
- •CONCLUDING THOUGHTS
- •Using Dictionaries
- •DICTIONARIES FOR NAME SCOPING
- •LOCAL DICTIONARIES
- •GLOBAL DICTIONARIES OF PROCEDURES
- •MAINTAINING THE DICTIONARY STACK
- •INTO AND OUT OF DICTIONARIES
- •LOOKING INTO DICTIONARIES
- •Using the forall Operator
- •Using the where and known Operators
- •REDEFINING OPERATORS
- •Changing the Behavior of Operators
- •Debugging with Redefined Names
- •Proper Nesting of Redefinitions
- •CONCLUDING THOUGHTS
- •Creating and Manipulating Data
- •CONSTRUCTING AN ARRAY
- •CONSTRUCTING A STRING
- •MANIPULATING DATA WITH PUT AND GET
- •CONCATENATING ARRAYS AND STRINGS
- •INPUT AND OUTPUT OF STRING DATA
- •ARRAYS VERSUS DICTIONARIES
- •ADVANCED TECHNIQUES
- •CONCLUDING THOUGHTS
- •Storing and Using Data
- •Data and the Operand Stack
- •Data and Algorithms for Underlining
- •CLASSICAL DATA STRUCTURES
- •Linked Lists
- •Using Arrays to Form Lists
- •Using Dictionaries to Form Lists
- •Queues, Trees, and Other Data Structures
- •CONCLUDING THOUGHTS
- •Program Data and Instructions
- •TURNING DATA INTO INSTRUCTIONS
- •TURNING INSTRUCTIONS INTO DATA
- •DATA CONVERSIONS
- •CONCLUDING THOUGHTS
- •File Objects
- •Streams and Files
- •PostScript File Operators
- •OPENING AND CLOSING FILES
- •READING AND WRITING FILES
- •Reading from a File
- •Writing to a File
- •Copying and Renaming Files
- •WRITING FORMATTED DATA TO FILES
- •Writing Out Various Data Types
- •Spaces, Tabs, Returns, and Special Characters
- •FILE STATUS INFORMATION
- •RANDOM VERSUS SEQUENTIAL ACCESS
- •CONCLUDING THOUGHTS
- •Appendix
- •Answers to Exercises

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 |