Overview of File Access

IDL provides powerful facilities for file input and output. Few restrictions are imposed on data files by IDL, and there is no unique IDL format. This chapter describes IDL input/output methods and routines and gives examples of programs which read and write data using IDL, C, and FORTRAN.

The first section of this chapter provides a description for how IDL input/output works. It is intentionally brief and is intended to serve only as an introduction. Additional details are covered in the following sections. For the IDL user, perhaps the largest single difference between platforms is input/output. The majority of this chapter covers information that is required in all of the environments IDL supports. Operating system specific information is concentrated in the final sections of this chapter.

About Opening Files

Before any file input or output can be performed, it is necessary to open a file. This is done using either the OPENR (Open for Reading), OPENW (Open for Writing), or OPENU (Open for Update) procedures. When a file is opened, it is associated with a Logical Unit Number, or LUN. All file input and output routines in IDL use the LUN rather than the filename, and most require that the LUN be explicitly specified. Once a file is opened, several input/output routines are available for use. Each routine fills a particular need – the one to use depends on the particular situation.

There are three exceptions to the need to open any file before performing input/output on it. Three files are always open – in fact, the user is not allowed to close them. These files are the standard input (usually the keyboard), the standard output (usually the IDL log window), and the standard error output (usually the terminal screen). These three files are associated with LUNs 0, -1, and -2, respectively. Because these files are always open, there is no need to open them prior to using them for input/output. The READ and PRINT procedures automatically use these files, so basic formatted input/output is extremely simple.

Simple I/O Examples

It is easy to use input/output using the default input and output files. The IDL command:

PRINT, 'Hello World.' 

causes IDL to print the line:

Hello World. 

on the terminal screen. This happens because PRINT formats its arguments and prints them to LUN -1, which is the standard output file. It is only slightly more complicated to use other files. The following IDL statements show how the above "Hello World" example could be sent to a file named hello.dat:

;Open LUN 1 for hello.dat with write access. 
OPENW, 1, 'hello.dat' 
 
;Do the output operation to the file. 
PRINTF, 1, 'Hello World.' 
 
;Close the file. 
CLOSE, 1  

Routines for Input/Output

See the categories under the functional heading "Input/Output" (IDL Quick Reference) for a complete list of available routines.