# File Unit Manipulations

The following sections describe common tasks when working with file units.

## Flushing File Units

For efficiency, IDL buffers its input/output in memory. Therefore, when data are output, there is a window of time during which data are in memory and have not been actually placed into the file. Normally, this behavior is transparent to the user (except for the improved performance). The FLUSH routine exists for those rare occasions where a program needs to be certain that the data has actually been written to the file immediately. For example, use the statement,

FLUSH, 1

to flush file unit one.

See FLUSHfor details.

## Positioning File Pointers

Each open file unit has a current file pointer associated with it. This file pointer indicates the position in the file at which the next input/output operation will take place. The file position is specified as the number of bytes from the start of the file. The first position in the file is position zero. The following statement will rewind file unit 1 to its start:

POINT_LUN, 1, 0

The following sequence of statements will position it at the end of the file:

tmp = FSTAT(1)

POINT_LUN, 1, tmp.size

POINT_LUN has the following operating-system specific behavior:

- UNIX: the current file pointer can be positioned arbitrarily – moving to a position beyond the current end-of-file causes the file to grow out to that point. The gap created is filled with zeroes.
- Windows: the current file pointer can be positioned arbitrarily – moving to a position beyond the current end-of-file causes the file to grow out to that point. Unlike UNIX, the gap created is filled with arbitrary data instead of zeroes.

See POINT_LUNfor details.

## Testing for End-Of-File

The EOF function is used to test a file unit to see if it is currently positioned at the end of the file. It returns true (1) if the end-of-file condition is true and false (0) otherwise.

For example, to read the contents of a file and print it on the screen, use the following statements:

;Open file demo.doc for reading.

OPENR, 1, 'demo.doc'

;Create a variable of type string.

LINE = ''

;Read and print each line until the end of the file is encountered.

WHILE(~ EOF(1)) DO BEGIN READF,1,LINE & PRINT,LINE & END

;Done with the file.

CLOSE, 1

See EOFfor details.